[Python-ideas] proto-PEP: Fixing Non-constant Default Arguments

tomer filiba 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
decorator.

as for uncopyable or stateful defaults (dev = sys.stdout), which require
reevaluation,
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
change
...             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))
...     device.flush()

this means you can do things like --

>>> say("hello1")
<stdout>: hello1
>>>
>>> say("hello2", device = sys.stderr)
<stderr>: hello2
>>>
>>> sys.stdout = sys.stderr
>>> say("hello3")
<stderr>: hello3
>>>
>>> sys.stdout = sys.__stdout__
>>> say("hello4")
<stdout>: hello4

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.


-tomer
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20070129/8f758c37/attachment.html>


More information about the Python-ideas mailing list