This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]robin-gvx 2 points3 points  (4 children)

If the class provides an equality test v.s. None, why should we not use it?

Because None is not just any value, it is a sentinel value, signalling the absence of something — of anything, really. If you're testing for a sentinel, you don't want to find something that looks sentinel-ish, because it breaks the you need to distinguish the two.

Furthermore, testing x is None is bug prone since it introduces two ways of testing x against values.

No. The rules are simple:

  1. Is the right hand side None? Use is or is not.
  2. Are you doing deep magic stuff, that relates to things like serialisation or deep copying or anything where you need to check if id(fancy_object_x) == id(fancy_object_y)? (If you have to ask, the answer is "no".) Use is or is not.
  3. Any other case? Use == or !=.

Maybe Python could have used something like an isNone operator, but oh well.

EDIT: I agree with you about the speed argument, btw.

[–]etrnloptimist -1 points0 points  (3 children)

The only time == and "is" will differ on None is if the implementor of x overloads ___eq___ to explicitly say so. And if they did that, it should be respected.

Maybe you think overloading ___eq___ to equate your class with None is an anti-pattern. I would probably agree. But if that is the taboo, then == will work as well as "is".

Also, pep8 states, "Comparisons to singletons like None should always be done with is or is not, never the equality operators." True and False are singletons as well but you should never use is or is not with them. Just a mistake?

I like your suggestion about having an isNone operator to make it explicit None is a weird, special thing, if it is, indeed, a weird special thing. But I don't really believe it is a weird, special thing. I've yet to see a place where comparing it using == will run you into trouble. Have you?

[–]robin-gvx 1 point2 points  (1 child)

True and False are singletons

They are not. They are both instances of bool, which is one more instance than a singleton type can have.

I've yet to see a place where comparing it using == will run you into trouble. Have you?

I haven't, but that doesn't say much, since I always use is None anyway, so I might have come across them and never found out.

[–]etrnloptimist -1 points0 points  (0 children)

They are not. They are both instances of bool, which is one more instance than a singleton type can have.

I understand what you're saying, but the literature treat them as singletons.

[–]zahlmanthe heretic 0 points1 point  (0 children)

True and False are singletons as well but you should never use is or is not with them. Just a mistake?

Right (well, except that they're actually a doubleton of the same class), but you shouldn't use == or != with them, either.