摘自 Fluent Python
evalsupport.py
print(' evalsupport module start')
def deco_alpha(cls):
print(' deco_alpha')
def inner_1(self):
print(' deco_alpha:inner_1')
cls.method_y = inner_1
return cls
class MetaAleph(type):
print(' MetaAleph body')
def __init__(cls, name, bases, dic):
print(' MetaAleph.__init__')
def inner_2(self):
print(' MetaAleph.__init__:inner_2')
cls.method_z = inner_2
print(' evalsupport module end')
evaltime.py
from evalsupport import deco_alpha
print(' evaltime module start')
class ClassOne():
print(' ClassOne body')
def __init__(self):
print(' ClassOne.__init__')
def __del__(self):
print(' ClassOne.__del__')
def method_x(self):
print(' ClassOne.method_x')
class ClassTwo(object):
print(' ClassTwo body')
@deco_alpha
class ClassThree():
print(' ClassThree body')
def method_y(self):
print(' ClassThree.method_y')
class ClassFour(ClassThree):
print(' ClassFour body')
if __name__ == '__main__':
print(' ClassOne tests', 30 * '.')
one = ClassOne()
one.method_x()
print(' ClassThree tests', 30 * '.')
three = ClassThree()
three.method_y()
print(' ClassFour tests', 30 * '.')
four = ClassFour()
four.method_y()
print(' evaltime module end')
evaltime_meta.py
from evalsupport import deco_alpha
from evalsupport import MetaAleph
print(' evaltime_meta module start')
@deco_alpha
class ClassThree():
print(' ClassThree body')
def method_y(self):
print(' ClassThree.method_y')
class ClassFour(ClassThree):
print(' ClassFour body')
def method_y(self):
print(' ClassFour.method_y')
class ClassFive(metaclass=MetaAleph):
print(' ClassFive body')
def __init__(self):
print(' ClassFive.__init__')
def method_z(self):
print(' ClassFive.method_y')
class ClassSix(ClassFive):
print(' ClassSix body')
def method_z(self):
print(' ClassSix.method_y')
if __name__ == '__main__':
print(' ClassThree tests', 30 * '.')
three = ClassThree()
three.method_y()
print(' ClassFour tests', 30 * '.')
four = ClassFour()
four.method_y()
print(' ClassFive tests', 30 * '.')
five = ClassFive()
five.method_z()
print(' ClassSix tests', 30 * '.')
six = ClassSix()
six.method_z()
print(' evaltime_meta module end')
>>> import evaltime
evalsupport module start
MetaAleph body # 说明类的 body 在导入时就执行
evalsupport module end
evaltime module start
ClassOne body
ClassTwo body # 类的 body 中有其他类, 也会被执行
ClassThree body # 先执行类的body, 然后将类作为类装饰器的参数
deco_alpha
ClassFour body # 类继承并不会继承类装饰器
evaltime module end
python evaltime.py
evalsupport module start
MetaAleph body
evalsupport module end
evaltime module start
ClassOne body
ClassTwo body
ClassThree body
deco_alpha
ClassFour body
ClassOne tests ..............................
ClassOne.__init__
ClassOne.method_x
ClassThree tests ..............................
deco_alpha:inner_1 # 装饰器修改了类属性
ClassFour tests ..............................
deco_alpha:inner_1 # ClassFour 继承的是经由装饰器修改后的ClassThree
evaltime module end
ClassOne.__del__ # 退出时垃圾回收
>>> import evaltime_meta
evalsupport module start
MetaAleph body
evalsupport module end
evaltime_meta module start
ClassThree body
deco_alpha
ClassFour body
ClassFive body
MetaAleph.__init__ # 先 ClassFour 定义, 然后交给其元类对他加工
ClassSix body
MetaAleph.__init__ # 先 ClassFour 定义, 然后交给其元类对他加工, 由于继承自 ClassFive, 其元类同上(注意区分基类与元类)
evaltime_meta module end
python3 evaltime_meta.py
evalsupport module start
MetaAleph body
evalsupport module end
evaltime_meta module start
ClassThree body
deco_alpha
ClassFour body
ClassFive body
MetaAleph.__init__
ClassSix body
MetaAleph.__init__
ClassThree tests ..............................
deco_alpha:inner_1
ClassFour tests ..............................
ClassFour.method_y
ClassFive tests ..............................
ClassFive.__init__
MetaAleph.__init__:inner_2
ClassSix tests ..............................
ClassFive.__init__
MetaAleph.__init__:inner_2
evaltime_meta module end