Upload
haoxun-zhan
View
307
Download
7
Embed Size (px)
Citation preview
浅析 python 对象模型huntzhan(詹昊恂)
1
导言
• 讲座主题与价值
• Python 中底层对象的结构
• 加深对 Python 的理解, 提前踩坑, 提升编码效率
• 关键问题: 在 Python 中,
• 什么是对象?
• 有几种对象? 它们之间是什么关系?
2
讲座的结构
• 必要的概念讲解: OOP术语/概念, 自省, Python 相关属性与函数
• <class 'type'> 与 <class 'object'>
• new-style class 与 classic class
• 对象模型的结构
• 总结
3
prerequisite一些基础概念
4
OOP Relationships
• superclass-subclass
relationships, 继承关系, 实线
• superclass/baseclass, 基类
• subclass/derived class, 派生类
• class-instance, 实例关系, 虚线
• class, 类
• instance, 实例
5
Python Example
class Super:
pass
class Sub(Super):
pass
class C:
pass
cobj = C()
6
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
object, 对象
• object 宽松定义. object 包含的信息
• Type
• Base Classes
• Identity, see id()
• Value
• Attributes
8
Everything is an object!
9
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
Introspection, 自省
• type introspection is the ability of a program to
examine the type or properties of an object at
runtime.
12
• examine the type
• type(object)
• object.__class__
• isinstance(object, classinfo)
• object.__bases__
• issubclass(class, classinfo)
• examine the properties
• dir([object])
• hasattr(object, name)
class Super:
pass
class Sub(Super):
pass
class C:
pass
Reflection, 反射
• the ability to manipulate the values, meta-data,
properties and/or functions of an object at runtime
15
• globals(), locals()
• object attribute manipulation
• understand data model, attributes and methods
• setattr, getattr, delta
16
<class 'type'> 与 <class 'object'>
17
• <class ‘type’>: type of all classes
• <class ‘object’>: base class of all *other* classes
18
19
烧脑一刻
• 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
烧脑一刻
21
more built-in classes
22
new object by
instantiation
23
new object by class
definition
24
new object by function definition
25
new-style class 与 classic class
26
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
对象模型中的位置
28
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
对象模型的结构
30
31
32
结构层次
• object:
• type object
• metaclass
• class
• non-type object(instance)
33
• 为什么要区分 metaclass 与 class?
• 控制 class 的创建过程, 支持更高层次的抽象手段
• 什么时候使用 metaclass ?
• “Never (as long as you're asking this question
anyway :)”
34
创建 metaclass
35
总结
36
什么是 object (对象)?
• Everything is an object!
• object 包含的 5 个信息.
37
object cheat sheet
• dri(cls): 包含 cls 以及其派生类(derived class)的集合.
38
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