13
說我這樣做:爲什麼類定義總是產生相同的字節碼?
#!/usr/bin/env python
# encoding: utf-8
class A(object):
pass
現在我拆開它:
python -m dis test0.py
4 0 LOAD_CONST 0 ('A')
3 LOAD_NAME 0 (object)
6 BUILD_TUPLE 1
9 LOAD_CONST 1 (<code object A at 0x1004ebb30, file "test0.py", line 4>)
12 MAKE_FUNCTION 0
15 CALL_FUNCTION 0
18 BUILD_CLASS
19 STORE_NAME 1 (A)
22 LOAD_CONST 2 (None)
25 RETURN_VALUE
現在我添加一些語句在類定義:
#!/usr/bin/env python
# encoding: utf-8
class A(object):
print 'hello'
1+1
pass
我再次拆解:
4 0 LOAD_CONST 0 ('A')
3 LOAD_NAME 0 (object)
6 BUILD_TUPLE 1
9 LOAD_CONST 1 (<code object A at 0x1004ebb30, file "test0.py", line 4>)
12 MAKE_FUNCTION 0
15 CALL_FUNCTION 0
18 BUILD_CLASS
19 STORE_NAME 1 (A)
22 LOAD_CONST 2 (None)
25 RETURN_VALUE
新的字節碼中不會出現什麼新的字符?
甜。不知道。有趣的問題和一個很好的答案。 (1) – NPE 2014-10-03 15:31:32