
As pointed out by Calvin Spealman in an earlier email: Calvin Spealman wrote:
Deep copy of the originally created default argument can be expensive and would not work in any useful way with non-literals as defaults, such as function calls or subscript lookups or even simple attributes.
However, your decorator is useful in several common cases. - Chris Rebert tomer filiba wrote:
i thought this code be solved nicely with a decorator... it needs some more work, but it would make a good cookbook recipe:
.>>> from copy import deepcopy. .>>> .>>> def defaults(**kwdefs): ... def deco(func): ... def wrapper(*args, **kwargs): ... for k,v in kwdefs.iteritems(): ... if k not in kwargs: ... kwargs[k] = deepcopy(v) ... return func(*args, **kwargs) ... return wrapper ... return deco ... .>>> @defaults(x = []) ... def foo(a, x): ... x.append(a) ... print x ... .>>> foo(5) [5] .>>> foo(5) [5] .>>> foo(5) [5]
maybe it should be done by copying func_defaults... then it could be written as
@copydefaults def f(a, b = 5, c = []): ...
-tomer _______________________________________________ Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas