default mutable arguments

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Thu Feb 8 15:20:29 EST 2007


Gigs_ a écrit :
> I read that this is not the same:
> if arg is None: arg = []
> arg = arg or []
> 
> 
> def functionF(argString="abc", argList = None):
>         if argList is None: argList = []      # < this
>         ...
> def functionF(argString="abc", argList=None):
>         argList = argList or []               # and this
>         ...
> 
> Why?

def test(arg=None):
   foo = arg or []
   print "arg : ", arg, " - foo : ", foo

test()
test(arg=0)
test(arg=False)
test(arg=())
test(arg={})
test(arg='')

etc...



More information about the Python-list mailing list