Re:WTF Python: Exploring and understanding Python through su...
by j0057 on Thursday February 11, 2021 @03:22AM
(#26099426)
Python should just remove the `is` operator. If you really need to compare object identities, which is already a rare scenario to begin with, you can explicitly compare the object IDs. Checking if something is or is not True/False/None can just be treating the thing as True-ish or False-ish. Making the language bigger is not nearly as impressive as making it smaller!
[ Parent
| on HN ]
Re:WTF Python: Exploring and understanding Python through su...
by dagw on Thursday February 11, 2021 @03:33AM
(#26099474)
I find checking for "is None" very useful, but that is also the only time I use 'is'. I try to avoid checking for True-ish and False-ish whenever possible, preferring to be more explicit.
Re:WTF Python: Exploring and understanding Python through su...
by shoo on Thursday February 11, 2021 @03:52AM
(#26099568)
I agree about explicit None checks. It is often useful to distinguish None from the zero value of a type such as a numeric zero, empty string or empty collection. I also write "is None" & "is not None" everywhere when doing these explicit checks.
Yet the same be benefits of explicit None checks can be attained by writing "== None" i.e. doing value based comparisons against a None value. So this argument does not explain why "is" is necessary.
Re:WTF Python: Exploring and understanding Python through su...
by pansa2 on Thursday February 11, 2021 @03:56AM
(#26099591)
I think the reason not to write `== None` is that classes can override `==`:
class C:
def __eq__(self, other): return True
C() == None # True
C() is None # False
Re:WTF Python: Exploring and understanding Python through su...
by eirki on Thursday February 11, 2021 @06:13AM
(#26100315)
It is also marginally faster, I think.
Re:WTF Python: Exploring and understanding Python through su...
by dagw on Thursday February 11, 2021 @09:50AM
(#26101992)
Just did a quick check, and looping over an array of 1 million Nones and checking each element
el==None
was roughly 25% slower than el is None
edit: Doing None==el
was roughly 15% slower
Re:WTF Python: Exploring and understanding Python through su...
by dragonwriter on Thursday February 11, 2021 @04:09AM
(#26099652)
> Yet the same be benefits of explicit None checks can be attained by writing "== None"
If you are going to do that's you should probably do a backwards comparison (`None==x`), otherwise you are calling x.__eq__(None), which probably won't be surprising in result if you have constrained things with sane typing, but still could be more expensive than `x is None`.
`x is None` reads a lot better though.
Re:WTF Python: Exploring and understanding Python through su...
by pansa2 on Thursday February 11, 2021 @03:49AM
(#26099555)
Yeah, IME `is None` is by far the most common use of `is`. I don’t think many people would miss `is` if it were removed and an `is_none` function introduced.
Re:WTF Python: Exploring and understanding Python through su...
by dragonwriter on Thursday February 11, 2021 @03:59AM
(#26099603)
> Checking if something is or is not True/False/None can just be treating the thing as True-ish or False-ish.
No, it can't. Especially obviously not something whose type is Optional[bool]. Or, more subtly, Optional[Number] or Optional[Sequence[T]].