3 ms·
Not really familiar with Python. Do you know why the second statement evaluates as false?
by dhosek 7y ago
Not really familiar with Python. Do you know why the second statement evaluates as false?
- mkl 7y agoSmall integer caching: https://wsvincent.com/python-wat-integer-cache/ https://wsvincent.com/python-wat-integer-cache/ The objects for small integers are built-in and static, but 17^2 is too big, and so new integer objects are created when the expressions are evaluated. "is" checks if two expressions represent the same object in memory. "==" checks for equality of values.
- xen0 7y agoCPython keeps an array for "small value" integers (I think in the range [-5, 256]). So when Python wants 256, it'll give you a reference to the existing object. Larger numbers require a brand new object.
- h8hawk 7y ago`is` is identity comparison and in python everything is "object". It check if two variable refer to same object (location in memory) or not. So you should not expect that `a is b` for `a=1.2 , b=1.2` be true. In this case actually first expression returned True is weird. because 0-256 objects are cached and every instance of them refer to same object.