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

Bruno Desthuilliers onurb at xiludom.gro
Tue Sep 5 04:02:40 EDT 2006


Carl Banks wrote:
> 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.
> 

Well... Since we all know it should be an identity test anyway... <g>



-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list