When is 0 not 0? When SymPy made it into an instance of its special Zero class of course! 😅
*AND* you can't convert that to a Decimal by just casting it either 😬
When is 0 not 0? When SymPy made it into an instance of its special Zero class of course! 😅
*AND* you can't convert that to a Decimal by just casting it either 😬
@jamescooke Showing the type was sufficient. After all:
Python 3.9.2 (default, Feb 28 2021, 17:03:44)
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license()" for more information.
>>> a = pow(2, 30, 1<<30)
>>> type(a)
<class 'int'>
>>> a
0
>>> a is 0
False
`is`-equality is not a necessary condition for type-and-value equality, even for interned types like int and str.
> You should not be writing code that relies on this behaviour in the first place.
I'm not.
That was just a screenshot of ipdb where I was debugging and surprised that an instance printed as "0" but wasn't the int 0.
Happy to add a screenshot with a better way of demonstrating that the value I had in the dictionary was `sp.S.Zero` and not `int(0)`, even though they `== 0` as `True` - what _should_ I have done?
@wizzwizz4 Testing zero with `is` is safe because it's got a fixed memory address: https://realpython.com/python-is-identity-vs-equality/#when-only-some-integers-are-interned
Try your code with 256 and 257 - you'll see that 256 gives True and 257 gives False.
@jamescooke It's not safe: that's an undocumented CPython implementation detail, and could change at any time. (Compare Java, where the equivalent behaviour is part of the language specification.) CPython is not the only implementation of Python.
You should not be writing code that relies on this behaviour in the first place. The decimal.Decimal limitation is a valid thing to be annoyed about (though it is documented), but is not working is not.
>>> 7832 is 7832
True
@jamescooke When is 7832 not 7832?
>>> x = 7832
>>> y = 7832
>>> x is y
False
You're holding is wrong.
GNU social JP is a social network, courtesy of GNU social JP管理人. It runs on GNU social, version 2.0.2-dev, available under the GNU Affero General Public License.
All GNU social JP content and data are available under the Creative Commons Attribution 3.0 license.