Function - Empty list as default parameter - not acting local?

Gregory Piñero gregpinero at gmail.com
Tue Nov 15 22:44:04 EST 2005


Hey guys, could anyone explain this behavior to me. It doesn't seem right
:-(

def testfunc(parm1,parm2={}):
print 'parm2',parm2
parm2['key1']=5

>>testfunc('greg')
parm2 {}
>>testfunc('greg')
parm2 {'key1': 5}

def testfunc2(parm1,parm2=[]):
print 'parm2',parm2
parm2.append(5)

>>testfunc2('greg')
parm2 []
>>testfunc2('greg')
parm2 [5]

If it's not obvious what's wrong with this. parm2 doesn't seem to be taking
on its default value. Maybe paramters aren't treated as local scope to the
function
and it's taking some kind of global status?

And finally, how would I do what I'm trying to achieve? ie If you don't
specifiy parm2 it should be an empty dictionary.

Thanks,

--
Gregory Piñero
Chief Innovation Officer
Blended Technologies
(www.blendedtechnologies.com <http://www.blendedtechnologies.com>)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20051115/848ee065/attachment.html>


More information about the Python-list mailing list