Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mrluanma
Created July 1, 2015 12:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrluanma/9118bf2ada09ebd017d3 to your computer and use it in GitHub Desktop.
Save mrluanma/9118bf2ada09ebd017d3 to your computer and use it in GitHub Desktop.
from __future__ import print_function
import dis
def f1():
x = 'a' * 20
y = 'a' * 20
return x is y
print(f1())
# True
dis.dis(f1)
# 7 0 LOAD_CONST 3 ('aaaaaaaaaaaaaaaaaaaa')
# 3 STORE_FAST 0 (x)
# 8 6 LOAD_CONST 4 ('aaaaaaaaaaaaaaaaaaaa')
# 9 STORE_FAST 1 (y)
# 9 12 LOAD_FAST 0 (x)
# 15 LOAD_FAST 1 (y)
# 18 COMPARE_OP 8 (is)
# 21 RETURN_VALUE
def f2():
x = 'a' * 21
y = 'a' * 21
return x is y
print(f2())
# False
dis.dis(f2)
# 27 0 LOAD_CONST 1 ('a')
# 3 LOAD_CONST 2 (21)
# 6 BINARY_MULTIPLY
# 7 STORE_FAST 0 (x)
# 28 10 LOAD_CONST 1 ('a')
# 13 LOAD_CONST 2 (21)
# 16 BINARY_MULTIPLY
# 17 STORE_FAST 1 (y)
# 29 20 LOAD_FAST 0 (x)
# 23 LOAD_FAST 1 (y)
# 26 COMPARE_OP 8 (is)
# 29 RETURN_VALUE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment