[Python-ideas] my take on mutable default arguments

tomer filiba tomerfiliba at gmail.com
Sat Jan 27 22:42:50 CET 2007


i think this is better:

.>>> from copy import deepcopy
.>>>
.>>> def copydefaults(func):
...     defaults = func.func_defaults
...     def wrapper(*args, **kwargs):
...         func.func_defaults = deepcopy(defaults)
...         return func(*args, **kwargs)
...     return wrapper
...
.>>> @copydefaults
... def f(a, x = []):
...     x.append(a)
...     print x
...
.>>>
.>>> f(1)
[1]
.>>> f(2)
[2]
.>>> f(3)
[3]



-tomer



More information about the Python-ideas mailing list