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
is behave different for integers in 3.6 and 3.7 #80773
Comments
# When you run the program: |
Python makes no guarantee as to whether an identity test on integers would return True or False. You should not depend on the behavior in any particular version. |
This is the sort of thing that makes me avoid "is" in favor of "==" for most applications. Understanding when two objects point to the same memory requires a deeper understanding of the underlying code than I usually want to delve into. Anyway, I find it interesting that for 3.7.3:
>>> a, b = 256, 256
>>> a is b
True
>>> a, b = 257, 257
>>> a is b
False So 2**8 is a magic number, for whatever reason. I'll be sticking with "=="... |
"So 2**8 is a magic number, for whatever reason." Actually, this is true. Accordingly to https://rushter.com/blog/python-integer-implementation/ "Optimization of commonly-used integers Interestingly enough, the PyLongObject structure takes at least 28 bytes for every allocated integer and therefore takes three times as much memory as a simple 64-bit C integer." This are constants #define NSMALLPOSINTS 257
#define NSMALLNEGINTS 5 You can find the code in: https://github.com/python/cpython/blob/master/Objects/longobject.c |
Well, then I guess that explains it! Still, like I said, I tend to shy away from features that require such a deep understanding of the implementation in order to avoid "gotchas". "is" does have its uses, but for me they very very rarely come up. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: