31.03.20 20:21, Andrew Barnert via Python-ideas пише:
Do you have code that you think actually _should_ be using is 1? Or code that 
you have to compile over and over (e.g., your deployment server doesn’t cache 
.pyc files, or you spawn new instances all the time without pre-built .pycs, or 
your app generates and compiles code on the fly, or …) and can’t change? If 
there are legitimate cases like those, I think that could definitely be a 
compelling argument against using SyntaxWarning here. But “the warning fired as 
intended and showed me a bug in my code” isn’t. And without more context, it’s 
hard to know which is the case here. So can you explain why this is a problem 
for you?

Twice in the last month I have been creating a PR for CPython which contains "is 1". I knew what I do.

One of them was in C. I needed very fast and simple test for integers 0 and 1, so I used the C equivalent of the "is" operator (compared pointers) with objects representing Python integers 0 and 1. And more, I used private CPython singletons _PyLong_Zero and _PyLong_One. There is no guarantee that all integer 0 an 1 are represented as these singletons (even in CPython), but it was only for optimization. In worst case I would lose an optimization but still got a correct result.

In second PR, I needed to determine if the optional argument with default value 1 was passed, and use fast path if it was not passed. "== 1" did not work, because 1.0 was error. It was first time when I encountered a SyntaxWarning I added myself. The workaround was simple: add a global `_ONE = 1` and use it as a default value and with the "is" operator. It would work even if 1 was not interned. In any way, this PR was rejected.

Conclusion? If you know what do you do you can find a workaround. But if you use this because you do not know difference between "is" and "==" and it works for you now, the warning can save you from possible bugs and teach you something.
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/PCVDL5UGVB4FUTK2NCT7YJGRJAUBPK7C/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to