[Tutor] how to unittest cli input
Steven D'Aprano
steve at pearwood.info
Tue Oct 13 01:19:33 CEST 2015
On Mon, Oct 12, 2015 at 10:37:51AM -0700, Alex Kleider wrote:
> Any comments about when/if to use 'if src != None:' vs 'if src is not
> None:'?
> (or 'if src == None:' vs 'if src is None:')
Short answer: always compare to None using `is` or `is not`.
Long answer:
If you want to check for src being specifically None, then use `src is
None`, since None is the only object that can be identical to None.
If you want to check for some random object that merely compares equal
to None (and why would you do that?) then use `src == None`, since that
will give src a chance to decide whether or not it compares equal to
None.
For built-in types (ints, floats, strings, etc.) there's no practical
difference, but as soon as custom objects may be involved, then you
don't know whether you might get some custom object that for reasons of
its own will compare equal to None:
class Falsey:
def __eq__(self, other):
return (not other)
Also, `is` comparisons are faster than `==` comparisons, not that this
will usually matter.
--
Steve
More information about the Tutor
mailing list