[Python-ideas] proto-PEP: Fixing Non-constant Default Arguments
tomerfiliba at gmail.com
Mon Jan 29 16:54:45 CET 2007
there really is no need to have something like that built into the language.
most default arguments are immutable anyhow, and i'd guess most of the
mutable defaults can be addressed with the suggested @copydefaults
as for uncopyable or stateful defaults (dev = sys.stdout), which require
you can just use this modified version of my first suggested decorator:
>>> def reeval(**kwdefs):
... def deco(func):
... def wrapper(*args, **kwargs):
... for k, v in kwdefs.iteritems():
... if k not in kwargs:
... kwargs[k] = v() # <--- this is the big
... return func(*args, **kwargs)
... return wrapper
... return deco
the defaults are now provided as *functions*, which are evaluated at the
time of calling the function.
>>> @reeval(device = lambda: sys.stdout)
... def say(text, device):
... device.write("%s: %s\n" % (device.name, text))
this means you can do things like --
>>> say("hello2", device = sys.stderr)
>>> sys.stdout = sys.stderr
>>> sys.stdout = sys.__stdout__
decorators are powerful enough for this not-so-common case.
it would be nice to have a collection of useful decorators as part of stdlib
(such as these ones, but including memoize and many others)...
but that's a different issue.
maybe we should really start a list of useful decorators to be included
as an stdlib module.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-ideas