Skip to content

Instantly share code, notes, and snippets.

@bcho
Created December 20, 2014 02:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bcho/010a9793776f3b89cdfd to your computer and use it in GitHub Desktop.
Save bcho/010a9793776f3b89cdfd to your computer and use it in GitHub Desktop.
trick
import dis
def item_instanate():
class Item:
pass
print(Item(), Item(), Item())
print(dis.dis(item_instanate))
item_instanate()
'''
6 0 LOAD_BUILD_CLASS
1 LOAD_CONST 1 (<code object Item at 0x7f9f8bd58150, file "item.py", line 6>)
4 LOAD_CONST 2 ('Item')
7 MAKE_FUNCTION 0
10 LOAD_CONST 2 ('Item')
13 CALL_FUNCTION 2 (2 positional, 0 keyword pair)
16 STORE_FAST 0 (Item)
9 19 LOAD_GLOBAL 0 (print)
22 LOAD_FAST 0 (Item)
25 CALL_FUNCTION 0 (0 positional, 0 keyword pair)
28 LOAD_FAST 0 (Item)
31 CALL_FUNCTION 0 (0 positional, 0 keyword pair)
34 LOAD_FAST 0 (Item)
37 CALL_FUNCTION 0 (0 positional, 0 keyword pair)
40 CALL_FUNCTION 3 (3 positional, 0 keyword pair)
43 POP_TOP
44 LOAD_CONST 0 (None)
47 RETURN_VALUE
None
<__main__.item_instanate.<locals>.Item object at 0x7f9f8bcb4cc0> <__main__.item_instanate.<locals>.Item object at 0x7f9f8bcb4cf8> <__main__.item_instanate.<locals>.Item object at 0x7f9f8bcb4eb8>
'''
import dis
def item_instanate():
class Item:
pass
print Item(), Item(), Item()
print dis.dis(item_instanate)
item_instanate()
'''
6 0 LOAD_CONST 1 ('Item')
3 LOAD_CONST 3 (())
6 LOAD_CONST 2 (<code object Item at 0x7f37cfa3b2b0, file "item_2.py", line 6>)
9 MAKE_FUNCTION 0
12 CALL_FUNCTION 0
15 BUILD_CLASS
16 STORE_FAST 0 (Item)
9 19 LOAD_FAST 0 (Item)
22 CALL_FUNCTION 0
25 PRINT_ITEM
26 LOAD_FAST 0 (Item)
29 CALL_FUNCTION 0
32 PRINT_ITEM
33 LOAD_FAST 0 (Item)
36 CALL_FUNCTION 0
39 PRINT_ITEM
40 PRINT_NEWLINE
41 LOAD_CONST 0 (None)
44 RETURN_VALUE
None
<__main__.Item instance at 0x7f37cf9ef950> <__main__.Item instance at 0x7f37cf9ef950> <__main__.Item instance at 0x7f37cf9ef950>
'''
from __future__ import print_function
import dis
def item_instanate():
class Item:
pass
print(Item(), Item(), Item())
print(dis.dis(item_instanate))
item_instanate()
'''
8 0 LOAD_CONST 1 ('Item')
3 LOAD_CONST 3 (())
6 LOAD_CONST 2 (<code object Item at 0x7f6aaed022b0, file "item_2_print_fn.py", line 8>)
9 MAKE_FUNCTION 0
12 CALL_FUNCTION 0
15 BUILD_CLASS
16 STORE_FAST 0 (Item)
11 19 LOAD_GLOBAL 0 (print)
22 LOAD_FAST 0 (Item)
25 CALL_FUNCTION 0
28 LOAD_FAST 0 (Item)
31 CALL_FUNCTION 0
34 LOAD_FAST 0 (Item)
37 CALL_FUNCTION 0
40 CALL_FUNCTION 3
43 POP_TOP
44 LOAD_CONST 0 (None)
47 RETURN_VALUE
None
<__main__.Item instance at 0x7f6aaecb6d40> <__main__.Item instance at 0x7f6aaecb6cf8> <__main__.Item instance at 0x7f6aaecb6dd0>
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment