|
本帖最后由 Z-zag 于 2019-4-18 17:16 编辑
- # evaltime.py
- from evalsupport import deco_alpha
- print('<[1]> evaltime 模块开始')
- class ClassOne:
- print('<[2]> classOne 定义体')
- def __init__(self):
- print('<[3]> classOne 的 __init__方法 ')
- def __del__(self):
- print('<[4]> classOne 的 __del__方法')
- def method_x(self):
- print('<[5]> classOne 的 method_x 方法')
- class ClassTwo:
- print('<[6]>ClassTwo 嵌套的类')
- @deco_alpha
- class ClassThree:
- print('<[7]> ClassThree 定义体')
- def method_y(self):
- print('<[8]> ClassThree 被装饰的方法 method_y')
- class ClassFour(ClassThree):
- print('<[9]> ClassFour 定义体')
- def method_y(self):
- print('<[10]>ClassFour 的 method_y 方法')
- if __name__ == '__main__':
- print('<[11]> ClassOne test', 30 * '.')
- one = ClassOne()
- one.method_x()
- print('<[12]> ClassThree test', 30 * '.')
- three = ClassThree()
- three.method_y()
- print('<[13]> ClassFour test', 30 * '.')
- four = ClassFour()
- four.method_y()
- print('<[14]> evaltime 模块结束')
复制代码- # evalsupport.py
- print('<[100]> evalsupport 模块开始')
- def deco_alpha(cls):
- print('<[200]> deco alpha函数定义体')
- def inner_1(self):
- print('<[300]> deco_alpha函数中的inner_1函数')
- cls.method_y = inner_1
- return cls
- class MetaAleph(type):
- print('<[400]> MetaAleph 类 的定义体')
- def __init__(cls, name, bases, dic):
- print('<[500]> MetaAlpha 的 __init__方法')
- def inner_2(self):
- print('<[600]> MetaAleph 的 init 中的方法inner_2')
- cls.method_z = inner_2
- print('<[700]> evalsupport模块结束的打印')
复制代码
分享一个小测试,测试一下 你对python解释器的执行有多少了解。
先不要运行代码, 自己算一算 输出应该是什么?
python解释器在 ‘导入时’ 和 ‘运行时’对代码的编译有何不同呢?
通过这个小测试,你将更加清楚的知道。
在终端中执行 python3 evaltime.py
在python shell中执行 >>>import evaltime
代码源自《流畅的python》 强烈推荐!!!
|
|