以Python为例讨论高级编程语言程序的wire format与校验
2010-09-22 11:26:23 来源:WEB开发网核心提示: 回顾之前分析.pyc文件格式时用的例子: demo.py: Python代码class A(): x = 1print(A.x) # 1# increment A.x by 2A.x += 2print(A.x) # 3# create an instance of Aa = A()pr
回顾之前分析.pyc文件格式时用的例子:
demo.py:
Python代码
class A():
x = 1
print(A.x) # 1
# increment A.x by 2
A.x += 2
print(A.x) # 3
# create an instance of A
a = A()
print(a.x) # 3
# increment A.x by 4
a.x += 4
print(a.x) # 7
print(A.x) # 3
demo.pyc:
Python bytecode代码
1 0 LOAD_CONST 0 ('A')
3 LOAD_CONST 5 (())
6 LOAD_CONST 1 (<code object A at 01237BA8, file "a.py", line 1>)
9 MAKE_FUNCTION 0
12 CALL_FUNCTION 0
15 BUILD_CLASS
16 STORE_NAME 0 (A)
4 19 LOAD_NAME 0 (A)
22 LOAD_ATTR 1 (x)
25 PRINT_ITEM
26 PRINT_NEWLINE
7 27 LOAD_NAME 0 (A)
30 DUP_TOP
31 LOAD_ATTR 1 (x)
34 LOAD_CONST 2 (2)
37 INPLACE_ADD
38 ROT_TWO
39 STORE_ATTR 1 (x)
8 42 LOAD_NAME 0 (A)
45 LOAD_ATTR 1 (x)
48 PRINT_ITEM
49 PRINT_NEWLINE
11 50 LOAD_NAME 0 (A)
53 CALL_FUNCTION 0
56 STORE_NAME 2 (a)
12 59 LOAD_NAME 2 (a)
62 LOAD_ATTR 1 (x)
65 PRINT_ITEM
66 PRINT_NEWLINE
15 67 LOAD_NAME 2 (a)
70 DUP_TOP
71 LOAD_ATTR 1 (x)
74 LOAD_CONST 3 (4)
77 INPLACE_ADD
78 ROT_TWO
79 STORE_ATTR 1 (x)
16 82 LOAD_NAME 2 (a)
85 LOAD_ATTR 1 (x)
88 PRINT_ITEM
89 PRINT_NEWLINE
17 90 LOAD_NAME 0 (A)
93 LOAD_ATTR 1 (x)
96 PRINT_ITEM
97 PRINT_NEWLINE
98 LOAD_CONST 4 (None)
101 RETURN_VALUE
更多精彩
赞助商链接