I can see usecases but clearly it should be used sparingly.
I can see usecases but clearly it should be used sparingly.
It’s interesting to learn about how the interpreter is implemented, but that’s about it.
Also, you are somewhat changing the topic from “what is an example of when you might want to is-compare two dicts”, no?
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.
Here’s a class that is equal to None, and everything else:
class EqualsEverything:
def __equals__(self, other: Any) -> bool:
return TrueJust curious: would it ever happen in practice?
sooner or later the __eq__ method will be redefined for some class, then reworked, and then.. == None might not be what was supposed to be..
or, my favorite, x='a' ; (x,)[0] == x[0] == x .. but are only equal until x changes to something not-1-long-sequence..
So, I use "is" since "is" is not a context dependent concept, like equals is. I've seen this once in the wild, and it made sense for its use:
def __eq__(self, other):
return bool(self) == bool(other)