None or 0

Michael Ströder michael at stroeder.com
Sat Sep 7 11:23:00 EDT 2002


HI!

I'm using the idiom

a = a or b

to get rid of statements like

if a is None:
   a = b

Now I wonder what happens if both a and b have zero-length values.

Trying in Python 2.2.1 reveals:

 >>> repr(0 or None)
'None'
 >>> repr(None or 0)
'0'
 >>> repr('' or None)
'None'
 >>> repr(None or '')
"''"
 >>>

Is it guaranteed to work like this or should that be avoided?

Ciao, Michael.




More information about the Python-list mailing list