Huh? func_defaults, default values in function calls

Roger Baklund roger at netconnect.no
Tue Apr 11 08:34:08 EDT 2000


>>> def a(x='',y='',z={}):
...     if x:
...       z['x'] = x
...     if y:
...       z['y'] = y
...     print str(z)
...
>>> a('a')
{'x': 'a'}
>>> a('b')
{'x': 'b'}
>>> a('b','c')
{'x': 'b', 'y': 'c'}
>>> a('a')
{'x': 'a', 'y': 'c'}
>>>

I don't get it. Why isn't the dictionary reset to {'x':'a'} when i call
the function the last time?

>>> a.func_defaults
('', '', {'x': 'a', 'y': 'c'})

Why is not {} the default for the z parameter?

--
Roger





More information about the Python-list mailing list