forked from Soyoung-Yoon/for_Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
python_dis연습.py
306 lines (279 loc) · 8.08 KB
/
python_dis연습.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
Python 3.7.6 (tags/v3.7.6:43364a7ae0, Dec 19 2019, 00:42:30) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> 123
123
>>> globals()
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>}
>>> 1234
1234
>>> globals()
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>}
>>> 123 + 456
579
>>> dis(123+456)
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
dis(123+456)
NameError: name 'dis' is not defined
>>> import dis
>>> dis(123+456)
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
dis(123+456)
TypeError: 'module' object is not callable
>>> dis.dis(123+456)
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
dis.dis(123+456)
File "C:\Python37-64\lib\dis.py", line 77, in dis
type(x).__name__)
TypeError: don't know how to disassemble int objects
>>> dis.dis(complie(123+456))
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
dis.dis(complie(123+456))
NameError: name 'complie' is not defined
>>> __builtins__
<module 'builtins' (built-in)>
>>> print(__builtins__)
<module 'builtins' (built-in)>
>>> import builtins
>>> print(builtins)
<module 'builtins' (built-in)>
>>> builtins.compile(123+456)
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
builtins.compile(123+456)
TypeError: compile() missing required argument 'filename' (pos 2)
>>> def func():
return 123+456
>>> dis.dis(func)
2 0 LOAD_CONST 1 (579)
2 RETURN_VALUE
>>> def func2():
a = 123
b = 456
return a + b
>>> dis.dis(func2)
2 0 LOAD_CONST 1 (123)
2 STORE_FAST 0 (a)
3 4 LOAD_CONST 2 (456)
6 STORE_FAST 1 (b)
4 8 LOAD_FAST 0 (a)
10 LOAD_FAST 1 (b)
12 BINARY_ADD
14 RETURN_VALUE
>>> def func3():
a = 123
return a + 456
>>> did.dis(func3)
Traceback (most recent call last):
File "<pyshell#29>", line 1, in <module>
did.dis(func3)
NameError: name 'did' is not defined
>>> dis.dis(func3)
2 0 LOAD_CONST 1 (123)
2 STORE_FAST 0 (a)
3 4 LOAD_FAST 0 (a)
6 LOAD_CONST 2 (456)
8 BINARY_ADD
10 RETURN_VALUE
>>> def func4():
a = 123 + 456
return a
>>> dis.dis(func4)
2 0 LOAD_CONST 1 (579)
2 STORE_FAST 0 (a)
3 4 LOAD_FAST 0 (a)
6 RETURN_VALUE
>>> def func5():
b = (10) + (20)
return b
>>> dis.dis(func5)
2 0 LOAD_CONST 1 (30)
2 STORE_FAST 0 (b)
3 4 LOAD_FAST 0 (b)
6 RETURN_VALUE
>>> def func5():
b = (100).__add__(200)
return b
>>> dis.dis(func5)
2 0 LOAD_CONST 1 (100)
2 LOAD_METHOD 0 (__add__)
4 LOAD_CONST 2 (200)
6 CALL_METHOD 1
8 STORE_FAST 0 (b)
3 10 LOAD_FAST 0 (b)
12 RETURN_VALUE
>>> def func6():
a = 100
b = 200
return a.__add__(b)
>>> dis.dis(func6)
2 0 LOAD_CONST 1 (100)
2 STORE_FAST 0 (a)
3 4 LOAD_CONST 2 (200)
6 STORE_FAST 1 (b)
4 8 LOAD_FAST 0 (a)
10 LOAD_METHOD 0 (__add__)
12 LOAD_FAST 1 (b)
14 CALL_METHOD 1
16 RETURN_VALUE
>>> b = dis.Bytecode(func)
>>> print(b)
Bytecode(<function func at 0x000001F0FDE671F8>)
>>> for x in b:
print(x.opname)
LOAD_CONST
RETURN_VALUE
>>> def func6():
b = 'abc' + 'def'
c = 'aaa'
return a + b
>>> dis.dis(func6)
2 0 LOAD_CONST 1 ('abcdef')
2 STORE_FAST 0 (b)
3 4 LOAD_CONST 2 ('aaa')
6 STORE_FAST 1 (c)
4 8 LOAD_GLOBAL 0 (a)
10 LOAD_FAST 0 (b)
12 BINARY_ADD
14 RETURN_VALUE
>>> def func7():
a = [1, 2, 3, 4]
return a + 5
>>> dis.dis(func7)
2 0 LOAD_CONST 1 (1)
2 LOAD_CONST 2 (2)
4 LOAD_CONST 3 (3)
6 LOAD_CONST 4 (4)
8 BUILD_LIST 4
10 STORE_FAST 0 (a)
3 12 LOAD_FAST 0 (a)
14 LOAD_CONST 5 (5)
16 BINARY_ADD
18 RETURN_VALUE
>>> def func8():
a = (1, 2, 3, 4)
return a + 5
>>> dis.dis(func8)
2 0 LOAD_CONST 1 ((1, 2, 3, 4))
2 STORE_FAST 0 (a)
3 4 LOAD_FAST 0 (a)
6 LOAD_CONST 2 (5)
8 BINARY_ADD
10 RETURN_VALUE
>>> def func9():
return {1,2,3,4}
>>> dis.dis(func9)
2 0 LOAD_CONST 1 (1)
2 LOAD_CONST 2 (2)
4 LOAD_CONST 3 (3)
6 LOAD_CONST 4 (4)
8 BUILD_SET 4
10 RETURN_VALUE
>>> def func10():
return {'a':10, 'b':20}
>>> dis.dis(func10)
2 0 LOAD_CONST 1 (10)
2 LOAD_CONST 2 (20)
4 LOAD_CONST 3 (('a', 'b'))
6 BUILD_CONST_KEY_MAP 2
8 RETURN_VALUE
>>> def func11():
return {1,2,3,4}
>>> dis.dis(func11)
2 0 LOAD_CONST 1 (1)
2 LOAD_CONST 2 (2)
4 LOAD_CONST 3 (3)
6 LOAD_CONST 4 (4)
8 BUILD_SET 4
10 RETURN_VALUE
>>> [1, 2, 3][2] = 10
>>> (1, 2, 3)[1] = 20
Traceback (most recent call last):
File "<pyshell#86>", line 1, in <module>
(1, 2, 3)[1] = 20
TypeError: 'tuple' object does not support item assignment
>>> a = 2020
>>> b = a
>>> c = a
>>> print(a, b, c)
2020 2020 2020
>>> a = 3000
>>> print(a, b, c)
3000 2020 2020
>>> import sys
>>> sys.getrefcount(a)
2
>>> sys.getrefcount(b)
3
>>> sys.getrefcount(sys)
103
>>> type(sys)
<class 'module'>
>>> isinstance(sys, type)
False
>>> isinstance(int, type)
True
>>> type(sys.getrefcount)
<class 'builtin_function_or_method'>
>>> sys.getrefcount
<built-in function getrefcount>
>>>
================== RESTART: C:/Python37-64/MyModules/name1.py ==================
30
300
>>>
================================ RESTART: Shell ================================
>>> a = 2020
>>> b = 2020
>>> import sys
>>> sys.getrefcount(a)
2
>>> sys.getrefcount(b)
2
>>> c = a
>>> sys.getrefcount(c)
3
>>> del a
>>> del b
>>> del c
>>> a = 2020
>>> b = a
>>> import sys
>>> sys.getrefcount(a)
3
>>> del a
>>> sys.getrefcount(b)
2
>>> del b
>>> a = 123
>>> b = 123
>>> import sys
>>> sys.getrefcount(b)
6
>>> a = 256
>>> sys.getrefcount(a)
71
>>> a = 2020
>>> b = a
>>> import sys
>>> sys.getrefcount(b)
3
>>> del a
>>> sys.getrefcount(a)
Traceback (most recent call last):
File "<pyshell#130>", line 1, in <module>
sys.getrefcount(a)
NameError: name 'a' is not defined
>>> del b
>>> a = 2020
>>> b = a
>>> import sys
>>> sys.getrefcount(b)
3
>>> del a
>>> sys.getrefcount(b)
2
>>>