39
浅析 python 对象模型 huntzhan(詹昊恂) 1

浅析 python 对象模型(zh_CN)

Embed Size (px)

Citation preview

Page 1: 浅析 python 对象模型(zh_CN)

浅析 python 对象模型huntzhan(詹昊恂)

1

Page 2: 浅析 python 对象模型(zh_CN)

导言

• 讲座主题与价值

• Python 中底层对象的结构

• 加深对 Python 的理解, 提前踩坑, 提升编码效率

• 关键问题: 在 Python 中,

• 什么是对象?

• 有几种对象? 它们之间是什么关系?

2

Page 3: 浅析 python 对象模型(zh_CN)

讲座的结构

• 必要的概念讲解: OOP术语/概念, 自省, Python 相关属性与函数

• <class 'type'> 与 <class 'object'>

• new-style class 与 classic class

• 对象模型的结构

• 总结

3

Page 4: 浅析 python 对象模型(zh_CN)

prerequisite一些基础概念

4

Page 5: 浅析 python 对象模型(zh_CN)

OOP Relationships

• superclass-subclass

relationships, 继承关系, 实线

• superclass/baseclass, 基类

• subclass/derived class, 派生类

• class-instance, 实例关系, 虚线

• class, 类

• instance, 实例

5

Page 6: 浅析 python 对象模型(zh_CN)

Python Example

class Super:

pass

class Sub(Super):

pass

class C:

pass

cobj = C()

6

Page 7: 浅析 python 对象模型(zh_CN)

type/class

• type/class 宽松定义

• represent abstract data types in programs

• can be subclassed(继承)

• can be instantiated(实例化)

• class(类) 等价于 type(类型), in Python >= 2.2

• 下文会混用 class 与 type, 不必惊慌, XD

7

Page 8: 浅析 python 对象模型(zh_CN)

object, 对象

• object 宽松定义. object 包含的信息

• Type

• Base Classes

• Identity, see id()

• Value

• Attributes

8

Page 9: 浅析 python 对象模型(zh_CN)

Everything is an object!

9

Page 10: 浅析 python 对象模型(zh_CN)

Everything Is an object!

• object 包括

• type object

• non-type object(instance)

• besides, instance

• can not be subclassed

• can not be instantiated

features type non-type

Type O O

Base

ClassesO X

Identity O O

Value X O

Attributes O O

10

Page 11: 浅析 python 对象模型(zh_CN)
Page 12: 浅析 python 对象模型(zh_CN)

Introspection, 自省

• type introspection is the ability of a program to

examine the type or properties of an object at

runtime.

12

Page 13: 浅析 python 对象模型(zh_CN)

• examine the type

• type(object)

• object.__class__

• isinstance(object, classinfo)

• object.__bases__

• issubclass(class, classinfo)

• examine the properties

• dir([object])

• hasattr(object, name)

Page 14: 浅析 python 对象模型(zh_CN)

class Super:

pass

class Sub(Super):

pass

class C:

pass

Page 15: 浅析 python 对象模型(zh_CN)

Reflection, 反射

• the ability to manipulate the values, meta-data,

properties and/or functions of an object at runtime

15

Page 16: 浅析 python 对象模型(zh_CN)

• globals(), locals()

• object attribute manipulation

• understand data model, attributes and methods

• setattr, getattr, delta

16

Page 17: 浅析 python 对象模型(zh_CN)

<class 'type'> 与 <class 'object'>

17

Page 18: 浅析 python 对象模型(zh_CN)

• <class ‘type’>: type of all classes

• <class ‘object’>: base class of all *other* classes

18

Page 19: 浅析 python 对象模型(zh_CN)

19

Page 20: 浅析 python 对象模型(zh_CN)

烧脑一刻

• 2a: If X is an instance of A, and A is a subclass of B, then

X is an instance of B as well.

• 2b: If B is an instance of M, and A is a subclass of B,

then A is an instance of M as well.

20

Page 21: 浅析 python 对象模型(zh_CN)

烧脑一刻

21

Page 22: 浅析 python 对象模型(zh_CN)

more built-in classes

22

Page 23: 浅析 python 对象模型(zh_CN)

new object by

instantiation

23

Page 24: 浅析 python 对象模型(zh_CN)

new object by class

definition

24

Page 25: 浅析 python 对象模型(zh_CN)

new object by function definition

25

Page 26: 浅析 python 对象模型(zh_CN)

new-style class 与 classic class

26

Page 27: 浅析 python 对象模型(zh_CN)

classic class

• classes defined with a class

statement that does not have

a built-in object amongst its

bases: either because it has

no bases, or because all of its

bases are classic classes

themselves - applying the

definition recursively.

• deprecated in Python 3.

27

Page 28: 浅析 python 对象模型(zh_CN)

对象模型中的位置

28

Page 29: 浅析 python 对象模型(zh_CN)

new-style class

• Introduced in Python 2.2.

• <class ‘type’>

• <class 'object'>

• “to remove most of the

differences between built-in

types and user-defined

classes”.

• always defines a new-style

class instead of classic

class!

29

Page 30: 浅析 python 对象模型(zh_CN)

对象模型的结构

30

Page 31: 浅析 python 对象模型(zh_CN)

31

Page 32: 浅析 python 对象模型(zh_CN)

32

Page 33: 浅析 python 对象模型(zh_CN)

结构层次

• object:

• type object

• metaclass

• class

• non-type object(instance)

33

Page 34: 浅析 python 对象模型(zh_CN)

• 为什么要区分 metaclass 与 class?

• 控制 class 的创建过程, 支持更高层次的抽象手段

• 什么时候使用 metaclass ?

• “Never (as long as you're asking this question

anyway :)”

34

Page 35: 浅析 python 对象模型(zh_CN)

创建 metaclass

35

Page 36: 浅析 python 对象模型(zh_CN)

总结

36

Page 37: 浅析 python 对象模型(zh_CN)

什么是 object (对象)?

• Everything is an object!

• object 包含的 5 个信息.

37

Page 38: 浅析 python 对象模型(zh_CN)

object cheat sheet

• dri(cls): 包含 cls 以及其派生类(derived class)的集合.

38

Page 39: 浅析 python 对象模型(zh_CN)

reading list• http://www.cafepy.com/article/python_types_and_objects/python_ty

pes_and_objects.html

• https://www.python.org/download/releases/2.2.3/descrintro/

• https://en.wikipedia.org/wiki/Type_introspection#Python

• https://en.wikipedia.org/wiki/Type_system#Static_and_dynamic_typ

e_checking_in_practice

• https://docs.python.org/3.4/library/types.html

• https://docs.python.org/3.5/reference/datamodel.html

• https://www.python.org/doc/newstyle/

39