Python对象创建的过程

  • Coder Van
  • 4 Minutes
  • June 7, 2017
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# coding: utf-8
"""
对象初始化过程及元类
元类主要使用type函数生成元类 参考: http://tips.pyhub.cc/zh/latest/2016-05-02-Class-and-Metaclass-ii/
type(object) -> the object's type
type(name, bases, dict) -> a new type
class 定义类的语法实际上转化为 type(name, bases, dict),其中 name 参数为类的名字,bases 为继承父类的元组,dict 为类的属性和方法:
"""
class A(object):
"""__new__ 不返回对象"""
print '* A01'
def __init__(self):
print "* A02", self
def __new__(cls, *args, **kwargs):
print "* A03", cls
return ''
class B(object):
"""__new__ 返回object对象"""
print '* B01'
def __init__(self):
print "* B02", self
def __new__(cls, *args, **kwargs):
print "* B03", cls
cv = object.__new__(cls, *args)
return cv
class MetaC(type):
"""元类 __new__ 返回要创建的类(C)的对象c"""
print '* MetaC01'
def __new__(mcs, cls, base, attrs):
print "* MetaC03", mcs, cls
return super(MetaC, mcs).__new__(mcs, cls, base, attrs)
class C(object):
__metaclass__ = MetaC
def __init__(self):
print "* C02", self
if __name__ == "__main__":
print '--------- A ---------'
a = A()
print '--------- B ---------'
b = B()
print '--------- C ---------'
c = C()