None or 0

Bengt Richter bokr at oz.net
Sat Sep 7 21:27:07 CEST 2002


On Sat, 07 Sep 2002 17:23:00 +0200, =?ISO-8859-1?Q?Michael_Str=F6der?= <michael at stroeder.com> wrote:

>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?
>

If you want a statement that sets a to a if a is not None and otherwise b,

    a = a or b

is not good, as you can infer from what you show above.

    a = (a, b)[a is None]

should do it, but

    if a is None: a = b

is better as a statement. If you need it in expression context, you could
use the

    (a, b)[a is None]	# alternatively, [a, b][a is None]

or maybe

    def myAorB(a, b):
        if a is None: return b
        return a
    ...
    myAorB(a, b)

For the following combinations, a or b obviously (when they're
distinguishable ;-) always produces b, and (a,b)[a is None] always
produces a except when a is None, in which case it produces b.

 >>> for a in [None, 0, '', [], (), {}]:
 ...     for b in [None, 0, '', [], (), {}]:
 ...         print 'a=%-5s b=%-8s a or b => %-8s  (a,b)[a is None] => %s' % (
 ...             `a`, `b`, `a or b`, `(a, b)[a is None]`
 ... )
 ...
 a=None  b=None     a or b => None      (a,b)[a is None] => None
 a=None  b=0        a or b => 0         (a,b)[a is None] => 0
 a=None  b=''       a or b => ''        (a,b)[a is None] => ''
 a=None  b=[]       a or b => []        (a,b)[a is None] => []
 a=None  b=()       a or b => ()        (a,b)[a is None] => ()
 a=None  b={}       a or b => {}        (a,b)[a is None] => {}
 a=0     b=None     a or b => None      (a,b)[a is None] => 0
 a=0     b=0        a or b => 0         (a,b)[a is None] => 0
 a=0     b=''       a or b => ''        (a,b)[a is None] => 0
 a=0     b=[]       a or b => []        (a,b)[a is None] => 0
 a=0     b=()       a or b => ()        (a,b)[a is None] => 0
 a=0     b={}       a or b => {}        (a,b)[a is None] => 0
 a=''    b=None     a or b => None      (a,b)[a is None] => ''
 a=''    b=0        a or b => 0         (a,b)[a is None] => ''
 a=''    b=''       a or b => ''        (a,b)[a is None] => ''
 a=''    b=[]       a or b => []        (a,b)[a is None] => ''
 a=''    b=()       a or b => ()        (a,b)[a is None] => ''
 a=''    b={}       a or b => {}        (a,b)[a is None] => ''
 a=[]    b=None     a or b => None      (a,b)[a is None] => []
 a=[]    b=0        a or b => 0         (a,b)[a is None] => []
 a=[]    b=''       a or b => ''        (a,b)[a is None] => []
 a=[]    b=[]       a or b => []        (a,b)[a is None] => []
 a=[]    b=()       a or b => ()        (a,b)[a is None] => []
 a=[]    b={}       a or b => {}        (a,b)[a is None] => []
 a=()    b=None     a or b => None      (a,b)[a is None] => ()
 a=()    b=0        a or b => 0         (a,b)[a is None] => ()
 a=()    b=''       a or b => ''        (a,b)[a is None] => ()
 a=()    b=[]       a or b => []        (a,b)[a is None] => ()
 a=()    b=()       a or b => ()        (a,b)[a is None] => ()
 a=()    b={}       a or b => {}        (a,b)[a is None] => ()
 a={}    b=None     a or b => None      (a,b)[a is None] => {}
 a={}    b=0        a or b => 0         (a,b)[a is None] => {}
 a={}    b=''       a or b => ''        (a,b)[a is None] => {}
 a={}    b=[]       a or b => []        (a,b)[a is None] => {}
 a={}    b=()       a or b => ()        (a,b)[a is None] => {}
 a={}    b={}       a or b => {}        (a,b)[a is None] => {}

Regards,
Bengt Richter



More information about the Python-list mailing list