python-01基础

  • python入门

在本章中你将
    学习到python语法与编码约定
    了解到函数式编程与面向对象编程
    学习如何编写装饰器
    了解python类多继承的MRO算法
  • The Zen of Python

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
  • 数据模型

在Python中数据被抽象成对象,Python程序中的所有数据都可以表示为对象或对象之间的关系。 每个对象都由3个部分组成:

  • identity,一旦被创建,就不会改变了,表示对象在内存中的地址。
  • type,对象的数据类型决定了对象支持的操作,以及value可变性,如numbers, strings and tuples 是可变类型, 而dictionaries
    ,lists是不可变的)。
  • value,表示对象的实际值。

Pytho常见数据类型可以分为:

  • None(真值为False)
  • Numbers(int,float,bool,complex)
  • Sequences(可变: list | 不可变: str,unicode,tuple)
  • Sets(set)
  • Mappings(dict)
  • 基础语法

关键字

  and       del       from      not       while
  as        elif      global    or        with
  assert    else      if        pass      yield
  break     except    import    print
  class     exec      in        raise
  continue  finally   is        return
  def       for       lambda    try

基本语法与约定

# 导入包
import scrapy as alias_name  # 导入了命名空间scrapy 取别名
访问属性时候必须带上命名空间
from __future__ import print_function  # 直接将公共的属性引入当前命名空间 直接使用属性名称访问
from xxxx import a,b,c 导入多个包
__import__() 使用内置函数导入包

# 命名约定
所谓”内部(Internal)”表示仅模块内可用, 或者, 在类内是保护或私有的.
用单下划线(_)开头表示模块变量或函数是protected的(使用import * from时不会包含).
用双下划线(__)开头的实例变量或方法表示类内私有.
将相关的类和顶级函数放在同一个模块里. 不像Java, 没必要限制一个类一个模块.
对类名使用大写字母开头的单词(如CapWords, 即Pascal风格), 但是模块名应该用小写加下划线的方式(如lower_with_under.py). 尽管已经有很多现存的模块使用类似于CapWords.py这样的命名, 但现在已经不鼓励这样做, 因为如果模块名碰巧和类名一致, 这会让人困扰.
类命名采取CamelCase驼峰法,方法采用snake_case

数据类型

# -*- coding:utf-8 -*-
from __future__ import print_function

print(type(1))
print(type(999999999999))
print(type(0.1))
print(type(True))
print(type('a'))
print(type("hello world"))
print(type([1, 1, 2, 3]))
print(type({1, 2, 3}))
print(type({1: 'a', 2: 'b'}))
print(type((1, 'a', "abc")))

# 枚举类型
from enum import Enum,unique
TimeUnit = Enum("TimeUnit", ("YEAR", "MONTH", "DAY", "HOUR", "MINUTE", "SECOND", "MILLS", "NANO"))
for unit, v in TimeUnit.__members__.items():
    print(unit, ":", v.value)
    
@unique  # 验证value 值唯一
class SomeType(Enum):
    A = "a"
    B = "b"
    C = "c"
print(SomeType.A.value)
print(SomeType.B)

控制结构

# 条件判断
if <条件判断1>:
    <执行1>
elif <条件判断2>:
    <执行2>
elif <条件判断3>:
    <执行3>
else:
    <执行4>

# 循环
for x in list:
    do_something()
    
while <真值表达式>:
    do_something()

异常处理

# python 内置异常层级
# https://docs.python.org/3/library/exceptions.html#exception-hierarchy

try:
    <语句>
except XError as e:
    <发生X异常发生执行的与语句>
except YError as e:
    <发生Y异常发生执行的与语句>
else:
    <没有发生异常会执行的语句>
finally:
    <无论如何都会执行的语句>

函数

from __future__ import print_function
from functools import partial

# 函数定义
# 位置参数,必选参数
def einstein_say(msg):
    print("Einstein said:", msg)

def newton_say(msg):
    print("Newton said:", msg)

# 默认参数,关键字参数
def someone_say(who="I", msg="Life is short, you need Python"):
    print(who, "said:", msg)

someone_say()
someone_say("Einstein", "E=MC^2")
someone_say("Newton", "Where is my Apple?")
someone_say(msg="slot_1 is mst", who="slot_2 is who")
someone_say(msg="nothing")
kw = {"who":"I", "msg": "kw args"}
someone_say(**kw)

# 不定参数 tuple
def sanzang_say(*msgs):
    for msg in msgs:
        print("TangSanZang said:", msg)
quotations = (u"阿弥陀佛", u"贫僧是从东土大唐而来去往西天拜佛求经的。", u"施主!", u"善哉!善哉!", u"悟空,为师错怪你了 。")
sanzang_say(*quotations)

# 匿名函数
inc = (lambda x: x + 1)
print(inc(1))
print(inc(2))

# 列表生成式
g = (x**3 for x in [1, 2, 3, 4, 5] if x > 1)
g.next()
for ele in g:
    print(ele)
print("%s- - - - - - - -%s" % ("|<", ">|"))    

# 列表表达式
x = [x**3 for x in [1, 2, 3] if x > 1]
for ele in x:
    print(ele)

# 装饰器 闭包 高阶函数
def aspect_log(**anno_args):
    def aspect_log_func(func):
        def func_wrapper(*args, **kwargs):
            print("start",anno_args["name"], "->", anno_args["order"])
            func(*args, **kwargs)
            print("end", anno_args["name"], "->", anno_args["order"])
        return func_wrapper
    return aspect_log_func

@aspect_log(name = "default", order = 2)
def simple_func():
    print("simple func do nothing")

simple_func()

# 偏函数
bin2Dec = partial(int, base=2)
hex2Dec = partial(int, base=16)
print(bin2Dec('1000'))
print(hex2Dec('400'))

import inspect

class Human(object):
    """
    示例类注释: 人类定义
    """
    def __init__(self, name):
        """示例方法注释: 初始构造函数"""
        super(Human, self).__init__()
        self._name = name
        self.__age = 18  # __开始的属性表示私有属性 外部不可访问 已变成_ClassName__age
        
    @staticmethod  # 静态方法
    def thinking():
        pass

    @classmethod  # 类方法
    def speak(cls):
        pass

    @property  # 属性
    def name(self):
        return self._name

    @name.setter
    def name(self, name):
        self._name = name

class Chinese(Human):
    pass

class HeNanPeople(Chinese):
    pass

class American(Human):
    pass

'''
        O       python2.2之前 经典类DFS
       / \      python2.2 新式类BFS
      /   \     python2.3后 C3算法
     A     B    
    / \   / \
   /   \ /   \
   C    D     E
    \   |    /
     \  |   / 
        F
        
方法解析顺序(Method Resolution Order,MRO)定义了多继承存在时 Python 解释器查找函数解析的正确方式。        

MRO 核心解决两个问题,【本地优先级】 和 【单调性】
DFS,BFS算法均无法解决单调性问题
C3 算法:
The name C3 refers to the three important properties of the resulting linearization: a consistent extended precedence graph, preservation of local precedence order, and fitting the monotonicity criterion. (The name "C3" is not an initialism.)

算法描述:
1. 如果第一个序列的第一个元素,是后续其他序列的第一个元素,或者不再后续序列中再次出现,则将这个元素合并到最终的方法解析顺序序列中,并从当前操作的全部序列中删除。
2. 如果不符合,则跳过此元素,查找下一个列表的第一个元素,重复1的判断规则

L(C) = [C] + merge(L(A),[A]) = [C,A,O]
L(D) = [D] + merge(L(A),L(B),[A,B])
     = [D] + merge([A,O], [B,O], [A,B])
     = [D,A,B,O]
L(E) = [E] + merge(L(B),[B]) = [E,B,O]
L(F) = [F] + merge(L(C), L(D), L(E), [C,D,E])
     = [F] + merge([C,A,O],[D,A,B,O],[E,B,O],[C, D,E])
     = [F, C] + merge([A,O], [D,A,B,O], [E,B,O],[D,E])
     = [F, C, D] + merge([A,O], [A,B,O], [E,B,O], [E])
     = [F, C, D, A] + merge([O], [B,O], [E,B,O],[E])
     = [F, C, D, A, E] + merge([O], [B,O], [B,O])
     = [F, C, D, A, E, B, O]
'''
class T(object):
    pass

class A(T):
    pass

class B(T):
    pass

class C(A, B):
    pass

ot = T()
oa = A()
print(type(ot), ot.__class__)
print(type(oa), oa.__class__)
print(inspect.getmro(C))  # 获取C的方法解析序列
  • 参考文档

[1] Python2.X官方文档
[2] C3算法详解
[3] 廖雪峰Python教程

你可能感兴趣的:(python-01基础)