4 ms·
I personally do use "if var is None:", but can't you just use "if var == None"?
by thrdbndndn 3y ago
I personally do use "if var is None:", but can't you just use "if var == None"?
- nomel 3y ago‘is’ checks if it the object ids are the same, with None having a unique one. Equals can be tricked. Here’s a class that is equal to None, and everything else: class EqualsEverything: def __equals__(self, other: Any) -> bool: return True
- thrdbndndn 3y agoThanks, I saw this on SO too. Just curious: would it ever happen in practice?
- svilen_dobrev 3y agoevery little thing happens in practice... usually unnoticed and buried while refactoring something innocent/ly. 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..
- nomel 3y agoI try to avoid statistical programming, preferring determinism, driven by intent. ;) 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)