摘自 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