一個針對 Python 語句執行順序的練習
摘自 Fluent Python
evalsupport.py
print('<[100]> evalsupport module start')
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 body')
def __init__(cls, name, bases, dic):
print('<[500]> MetaAleph.__init__')
def inner_2(self):
print('<[600]> MetaAleph.__init__:inner_2')
cls.method_z = inner_2
print('<[700]> evalsupport module end')
evaltime.py
from evalsupport import deco_alpha
print('<[1]> evaltime module start')
class ClassOne():
print('<[2]> ClassOne body')
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(object):
print('<[6]> ClassTwo body')
@deco_alpha
class ClassThree():
print('<[7]> ClassThree body')
def method_y(self):
print('<[8]> ClassThree.method_y')
class ClassFour(ClassThree):
print('<[9]> ClassFour body')
if __name__ == '__main__':
print('<[11]> ClassOne tests', 30 * '.')
one = ClassOne()
one.method_x()
print('<[12]> ClassThree tests', 30 * '.')
three = ClassThree()
three.method_y()
print('<[13]> ClassFour tests', 30 * '.')
four = ClassFour()
four.method_y()
print('<[14]> evaltime module end')
evaltime_meta.py
from evalsupport import deco_alpha
from evalsupport import MetaAleph
print('<[1]> evaltime_meta module start')
@deco_alpha
class ClassThree():
print('<[2]> ClassThree body')
def method_y(self):
print('<[3]> ClassThree.method_y')
class ClassFour(ClassThree):
print('<[4]> ClassFour body')
def method_y(self):
print('<[5]> ClassFour.method_y')
class ClassFive(metaclass=MetaAleph):
print('<[6]> ClassFive body')
def __init__(self):
print('<[7]> ClassFive.__init__')
def method_z(self):
print('<[8]> ClassFive.method_y')
class ClassSix(ClassFive):
print('<[9]> ClassSix body')
def method_z(self):
print('<[10]> ClassSix.method_y')
if __name__ == '__main__':
print('<[11]> ClassThree tests', 30 * '.')
three = ClassThree()
three.method_y()
print('<[12]> ClassFour tests', 30 * '.')
four = ClassFour()
four.method_y()
print('<[13]> ClassFive tests', 30 * '.')
five = ClassFive()
five.method_z()
print('<[14]> ClassSix tests', 30 * '.')
six = ClassSix()
six.method_z()
print('<[15]> evaltime_meta module end')
>>> import evaltime
<[100]> evalsupport module start
<[400]> MetaAleph body # 說明類的 body 在匯入時就執行
<[700]> evalsupport module end
<[1]> evaltime module start
<[2]> ClassOne body
<[6]> ClassTwo body # 類的 body 中有其他類, 也會被執行
<[7]> ClassThree body # 先執行類的body, 然後將類作為類裝飾器的引數
<[200]> deco_alpha
<[9]> ClassFour body # 類繼承並不會繼承類裝飾器
<[14]> evaltime module end
python evaltime.py
<[100]> evalsupport module start
<[400]> MetaAleph body
<[700]> evalsupport module end
<[1]> evaltime module start
<[2]> ClassOne body
<[6]> ClassTwo body
<[7]> ClassThree body
<[200]> deco_alpha
<[9]> ClassFour body
<[11]> ClassOne tests ..............................
<[3]> ClassOne.__init__
<[5]> ClassOne.method_x
<[12]> ClassThree tests ..............................
<[300]> deco_alpha:inner_1 # 裝飾器修改了類屬性
<[13]> ClassFour tests ..............................
<[300]> deco_alpha:inner_1 # ClassFour 繼承的是經由裝飾器修改後的ClassThree
<[14]> evaltime module end
<[4]> ClassOne.__del__ # 退出時垃圾回收
>>> import evaltime_meta
<[100]> evalsupport module start
<[400]> MetaAleph body
<[700]> evalsupport module end
<[1]> evaltime_meta module start
<[2]> ClassThree body
<[200]> deco_alpha
<[4]> ClassFour body
<[6]> ClassFive body
<[500]> MetaAleph.__init__ # 先 ClassFour 定義, 然後交給其元類對他加工
<[9]> ClassSix body
<[500]> MetaAleph.__init__ # 先 ClassFour 定義, 然後交給其元類對他加工, 由於繼承自 ClassFive, 其元類同上(注意區分基類與元類)
<[15]> evaltime_meta module end
python3 evaltime_meta.py
<[100]> evalsupport module start
<[400]> MetaAleph body
<[700]> evalsupport module end
<[1]> evaltime_meta module start
<[2]> ClassThree body
<[200]> deco_alpha
<[4]> ClassFour body
<[6]> ClassFive body
<[500]> MetaAleph.__init__
<[9]> ClassSix body
<[500]> MetaAleph.__init__
<[11]> ClassThree tests ..............................
<[300]> deco_alpha:inner_1
<[12]> ClassFour tests ..............................
<[5]> ClassFour.method_y
<[13]> ClassFive tests ..............................
<[7]> ClassFive.__init__
<[600]> MetaAleph.__init__:inner_2
<[14]> ClassSix tests ..............................
<[7]> ClassFive.__init__
<[600]> MetaAleph.__init__:inner_2
<[15]> evaltime_meta module end
相關文章
- for語句執行順序
- SQL 語句的執行順序SQL
- mySQL 執行語句執行順序MySql
- SQL語句執行順序SQL
- 一個 MySQL sql 語句執行順序帶來的 bugMySql
- Select語句執行順序
- SQL語句各子句的執行順序SQL
- SQL Server SQL語句執行順序SQLServer
- SQL語句中SELECT語句的執行順序SQL
- SQL 查詢語句的執行順序解析SQL
- Hive SQL語句的正確執行順序HiveSQL
- Java for迴圈中語句執行的順序Java
- T-sql語句查詢執行順序SQL
- SQL Select語句邏輯執行順序SQL
- mysql 中sql語句關鍵字的書寫順序與執行順序MySql
- JavaScript for迴圈語句的執行順序和優化JavaScript優化
- sql語句執行順序與效能優化(1)SQL優化
- SQL Server中SELECT語句執行順序解析SQLServer
- for語句括號中程式碼執行順序介紹
- Oracle SQL語句執行流程與順序原理解析OracleSQL
- SELECT語句邏輯執行順序 你知道嗎?
- SQL語句執行順序及建議--歸納整理SQL
- MySQL 查詢常用操作(0) —— 查詢語句的執行順序MySql
- python if語句有先後順序嗎Python
- java中異常處理finally和return語句的執行順序Java
- sql select語法執行順序SQL
- JavaScript的執行順序JavaScript
- SQL語句中的AND和OR執行順序問題SQL
- 關於觸發器在行級和語句級的執行順序問題觸發器
- Sql執行順序SQL
- mysql自定義排序順序語句MySql排序
- python基礎語句小練習Python
- 一文搞懂Python Unittest測試方法執行順序Python
- JavaScript執行順序分析JavaScript
- 任務執行順序
- laravel Event執行順序Laravel
- 面試題思考:try 程式碼塊中含 return 語句時,程式碼執行順序面試題
- 關於 Promise 的執行順序Promise