Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why? a is b #100

Open
anhuaxiang opened this issue Nov 30, 2018 · 9 comments

Comments

@anhuaxiang
Copy link

commented Nov 30, 2018

python: 3.7.1
image

@satwikkansal satwikkansal added enhancement and removed enhancement labels Dec 4, 2018

@satwikkansal

This comment has been minimized.

Copy link
Owner

commented Dec 4, 2018

Interesting

image

@satwikkansal

This comment has been minimized.

Copy link
Owner

commented Dec 4, 2018

Let me dig deeper to check what changed in Python 3.7 that's causing this.

@prehensilecode

This comment has been minimized.

Copy link

commented Dec 9, 2018

Python 3.7.1 (default, Dec  7 2018, 14:57:25) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-23)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a,b = 257,257
>>> a is b
False
>>> a = 257
>>> b = 257
>>> a is b
False
>>> a = 257 ; b = 257
>>> a is b
True
@davidlatwe

This comment has been minimized.

Copy link

commented Dec 9, 2018

But if using int() to convert

Python 3.7.1 (default, Nov 24 2018, 22:14:32) [MSC v.1912 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> a, b = 257, 257
>>> a is b
False
>>> a, b = int(257), int(257)
>>> a is b
True
@morgan008

This comment has been minimized.

Copy link

commented Dec 13, 2018

@satwikkansal

Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> a, b = "wtf!", "wtf!"
>>> a is b
False
@free-free

This comment has been minimized.

Copy link

commented Apr 17, 2019

Let me give a summary for the first example "Strings can be tricky sometimes *".

>>> a, b = "wtf!", "wtf!'
>>> a is b
True # in Python < 3.7
False # in Python >= 3.7

>>> 'a' * 20 is 'aaaaaaaaaaaaaaaaaaaa'
True  # in all Python versions
>>> 'a' * 21 is   'aaaaaaaaaaaaaaaaaaaaa'
False # in Python < 3.7
True  # in Python >= 3.7
@selfboot

This comment has been minimized.

Copy link

commented Apr 18, 2019

Python 3.7.3 (default, Mar 27 2019, 09:23:39)
[Clang 10.0.0 (clang-1000.11.45.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> a, b = 257, 257
>>> a is b
False
>>> a = 257; b = 257;
>>> a is b
True
>>> a, b = int(257), int(257)
>>> a is b
True
>>> a = 257
>>> b = 257
>>> a is b
False
@satwikkansal

This comment has been minimized.

Copy link
Owner

commented Jun 8, 2019

Okay, to summarize

>>> a, b = 257, 257
>>> a is b
True # Python 3.6 or less
False # Python 3.7

>>> a = 257; b =257;
>>> a is b
False # Python 3.6 or less
True # Python 3.7

I tried disassembling the code, but couldn't find the reason for this difference

import dis
def some_func():
	a, b = 257, 257
	a is b

dis.dis(some_func)

# Python 3.6 

  2           0 LOAD_CONST               2 ((257, 257))
              2 UNPACK_SEQUENCE          2
              4 STORE_FAST               0 (a)
              6 STORE_FAST               1 (b)

  3           8 LOAD_FAST                0 (a)
             10 LOAD_FAST                1 (b)
             12 COMPARE_OP               8 (is)
             14 POP_TOP
             16 LOAD_CONST               0 (None)
             18 RETURN_VALUE

# Python 3.7

  2           0 LOAD_CONST               1 ((257, 257))
              2 UNPACK_SEQUENCE          2
              4 STORE_FAST               0 (a)
              6 STORE_FAST               1 (b)

  3           8 LOAD_FAST                0 (a)
             10 LOAD_FAST                1 (b)
             12 COMPARE_OP               8 (is)
             14 POP_TOP
             16 LOAD_CONST               0 (None)
             18 RETURN_VALUE

LOAD_CONST is being called only once in both the cases. Any help in figuring out the exact change which led to this difference is appreciated 😅

satwikkansal added a commit that referenced this issue Jun 8, 2019
@satwikkansal

This comment has been minimized.

Copy link
Owner

commented Jun 9, 2019

Similar results come when we replace a and b by wtf! causing another example to become outdated #120

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
7 participants
You can’t perform that action at this time.