12.31 python
1.python 类
1.1 面向过程与面向对象
•对象:一切事物可以解释为对象(Object), 对象是集属性和方法(行为)于一体的概念,人们仅通过描述其属性和行为来定义某个特定的对象。
•面向对象:把构成问题事物分解成对象,然后将待解决的问题描述为对象解决问题的行为。
•面向过程:分析问题的解决思路,一步一步将这些步骤实现。
1.2 python的面向对象
•一切皆为对象(Object),对象既有属性也有功能。
包括 list, dictionary.
•类(Class) 是对象的抽象化。
狗(抽象)->哈士奇(具体)
•先有类,后有对象
1.3 类的代码实现
1.3.1 类的定义
1 | Class MyClass: |
1.3.2 类对象
如果实例化的时候需要指定特定的状态则需要定义一个方法 init():
1 | #无参数的__init__ |
2 | def __init__(self): |
3 | self.data=[] |
4 | #有参数的__init__ |
5 | class Complex: |
6 | def __init__(self, realpart, imagpart): |
7 | self.r = realpart |
8 | self.i = imagpart |
9 | ... |
10 | 3.0, -4.5) x = Complex( |
11 | x.r, x.i |
12 | (3.0, -4.5) |
1.3.3 实例对象
1 | x.counter = 1 |
2 | while x.counter < 10: |
3 | x.counter = x.counter * 2 |
4 | print(x.counter) |
5 | del x.counter |
1.3.4 类变量和实例变量
1 | class Dog: |
2 | |
3 | kind = 'canine' # class variable shared by all instances |
4 | |
5 | def __init__(self, name): |
6 | self.name = name # instance variable unique to each instance |
7 | |
8 | 'Fido') d = Dog( |
9 | 'Buddy') e = Dog( |
10 | # shared by all dogs d.kind |
11 | 'canine' |
12 | # shared by all dogs e.kind |
13 | 'canine' |
14 | # unique to d d.name |
15 | 'Fido' |
16 | # unique to e e.name |
17 | 'Buddy' |
不是类的共同属性,不要在类变量中定义
1 | class Dog: |
2 | |
3 | tricks = [] # mistaken use of a class variable |
4 | |
5 | def __init__(self, name): |
6 | self.name = name |
7 | |
8 | def add_trick(self, trick): |
9 | self.tricks.append(trick) |
10 | |
11 | 'Fido') d = Dog( |
12 | 'Buddy') e = Dog( |
13 | 'roll over') d.add_trick( |
14 | 'play dead') e.add_trick( |
15 | # unexpectedly shared by all dogs d.tricks |
16 | ['roll over', 'play dead'] |
如何修改代码?
1.3.5 优先级
*如果类和实例中有共同的属性名,则优先访问实例的属性。
1 | class Warehouse: |
2 | purpose = 'storage' |
3 | region = 'west' |
4 | |
5 | w1 = Warehouse() |
6 | print(w1.purpose, w1.region) |
7 | storage west |
8 | w2 = Warehouse() |
9 | 'east' w2.region = |
10 | print(w2.purpose, w2.region) |
11 | storage east |
有一下几点需要注意:
*python 类没有控制访问权限的机制,所有类的数据都可以获取到
*类的所有方法(函数)的第一个参数为self(基于约定),没有特殊含义。
*类方法可以在类外部定义(不推荐)
1 | # Function defined outside the class |
2 | def f1(self, x, y): |
3 | return min(x, x+y) |
4 | |
5 | class C: |
6 | f = f1 |
7 | |
8 | def g(self): |
9 | return 'hello world' |
10 | |
11 | h = g |
*通过self调用类的其他属性和方法
1 | class Bag: |
2 | def __init__(self): |
3 | self.data = [] |
4 | |
5 | def add(self, x): |
6 | self.data.append(x) |
7 | |
8 | def addtwice(self, x): |
9 | self.add(x) |
10 | self.add(x) |
1.3.6 类的继承(optional)和多重继承
1 | class DerivedClassName(BaseClassName): |
2 | <statement-1> |
3 | . |
4 | . |
5 | . |
6 | <statement-N> |
*派生类和基类。派生类可以覆盖基类的方法,
1 | class DerivedClassName(Base1, Base2, Base3): |
2 | <statement-1> |
3 | . |
4 | . |
5 | . |
6 | <statement-N> |
1.3.7 私有变量
1 | class Mapping: |
2 | def __init__(self, iterable): |
3 | self.items_list = [] |
4 | self.__update(iterable) |
5 | |
6 | def update(self, iterable): |
7 | for item in iterable: |
8 | self.items_list.append(item) |
9 | |
10 | __update = update # private copy of original update() method |
11 | |
12 | class MappingSubclass(Mapping): |
13 | |
14 | def update(self, keys, values): |
15 | # provides new signature for update() |
16 | # but does not break __init__() |
17 | for item in zip(keys, values): |
18 | self.items_list.append(item) |
1.4 练习
2.代码
2.1 迭代器 (iter返回迭代器对象)
1 | 'abc' s = |
2 | it = iter(s) |
3 | it |
4 | <iterator object at 0x00A1DB50> |
5 | next(it) |
6 | 'a' |
7 | next(it) |
8 | 'b' |
9 | next(it) |
10 | 'c' |
11 | next(it) |
12 | Traceback (most recent call last): |
13 | File "<stdin>", line 1, in <module> |
14 | next(it) |
15 | StopIteration |
2.2 置换
1 | >>>a=[1,2,3,4,5] |
2 | >>>b=a |
3 | >>>b[0]=5 |
4 | >>>b |
5 | ? |
6 | >>>a |
7 | ? |
1 | >>>a=5 |
2 | >>>b=a |
3 | >>>b=3 |
4 | >>>b |
5 | ? |
6 | >>>a |
7 | ? |
2.3 递归
斐波拉契数列: 0,1,1,2,3,5,8,13,21……
1 | def fib_list(n) : |
2 | if n == 1 or n == 2 : |
3 | return 1 |
4 | else : |
5 | m = fib_list(n - 1) + fib_list(n - 2) |
6 | return m |
2.4 模块、库和包
module: 一个py文件就是一个module,代码片段
package 自带init.py 的文件夹。
练习:创建一个模块/包
2.5 常用函数
map/reduce/lambda