To be honest this response underscores the problem with pages like this. In Python, strings and numbers are objects, and “is” tells you if they are the same object. You wouldn’t compare strings or numbers in C using a pointer comparison, and you shouldn’t do it in Python either. The fact that it works sometimes in cpython is a coincidence.
It’s interesting to learn about how the interpreter is implemented, but that’s about it.
You should absolutely be using `is` where appropriate. `x is None` is almost always preferable to `x == None`. If you're checking for object identity, use `is`. If equality, use `==`. They're different use cases.
How is that your response, after reading this article? The is operator checks whether the two items are the same object, which is critical in some circumstances.
You are correct. I kind of took `is None` for granted as it just feels boilerplate when coding in Python.
Although I have written over a hundred thousand of lines of code in Python over the years; I use Python mostly for dev ops tooling, reporting, monitoring and automation so they don't get super complex and they mostly can lean on procedural programming patterns.
I could imagine complex frameworks needing heavy use of Objects that could lean on the 'is' keyword.
wzdd|2 years ago
It’s interesting to learn about how the interpreter is implemented, but that’s about it.
kstrauser|2 years ago
maweki|2 years ago
JoBrad|2 years ago
wodenokoto|2 years ago
stevesimmons|2 years ago
Timothycquinn|2 years ago
Although I have written over a hundred thousand of lines of code in Python over the years; I use Python mostly for dev ops tooling, reporting, monitoring and automation so they don't get super complex and they mostly can lean on procedural programming patterns.
I could imagine complex frameworks needing heavy use of Objects that could lean on the 'is' keyword.
thrdbndndn|2 years ago