471,055 Members | 1,653 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,055 software developers and data experts.

default mutable arguments

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?
thanks !!!
Feb 8 '07 #1
2 1038
Gigs_ wrote:
I read that this is not the same:
def functionF(argString="abc", argList = None):
if argList is None: argList = [] # < this
...
def functionF(argString="abc", argList=None):
argList = argList or [] # and this
...

Why?
If argList is a false value besides None ("", [], {}, False, etc.), the
second example will replace it with an empty list.
Feb 8 '07 #2
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...
Feb 8 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.