testing for valid reference: obj vs. None!=obs vs. obj is not None

Carl Banks pavlovevidence at gmail.com
Mon Sep 4 23:47:51 EDT 2006


Bruno Desthuilliers wrote:
> In python, assignement is a statement, not an expression, so there's no
> way you could write 'if obj = None' by mistake (-> syntax error). So
> this style is unpythonic. Also, None is a singleton, and identity test
> is way faster than equality test.

Playing Devil's advocate here: if you were to write "x!=None", then x's
__eq__ method is invoked, which might not account for the possibility
that the other operand is None.

However, if you write "None!=x", then None's __eq__ method is invoked,
which accounts for any given type.


Carl Banks




More information about the Python-list mailing list