import dis
def testMethod1():
a, b = 300, 300
print dis.dis(testMethod1)
Prints:
4 0 LOAD_CONST 2 ((300, 300))
3 UNPACK_SEQUENCE 2
6 STORE_FAST 0 (a)
9 STORE_FAST 1 (b)
12 LOAD_CONST 0 (None)
15 RETURN_VALUE None
def testMethod2():
a = 300
b = 300
Prints:
7 0 LOAD_CONST 1 (300)
3 STORE_FAST 0 (a)
8 6 LOAD_CONST 1 (300)
9 STORE_FAST 1 (b)
12 LOAD_CONST 0 (None)
15 RETURN_VALUE None
So, it looks essentially the same, but with LOAD_CONST
in one step in the first method and two steps in the second method....
EDIT
After some testing, I discovered that both methods return False
eventually; however, on one run only, ie not putting the methods in a loop, they seem to always return True
. Sometimes it uses a single reference, and sometimes it does not.
The documentation only states that -5 to 256 will return the same reference; hence, you simply just shouldn't be using is
for comparison (in this case) as the number's current id
has no guarantee on it.
NB: You never want to use is
for comparison of values, as that's not what it's for, it's to compare identities. My point was that is
's return value is not always going to be True
when you're outside of the defined range.