[Python-ideas] Default arguments in Python - the return - running out of ideas but...
Carl Johnson
cmjohnson.mailinglist at gmail.com
Wed May 20 03:04:27 CEST 2009
Pascal Chambon wrote:
> Without new keyword or operator, a good looking solution for dynamic
> defaults is unlikely to appear, imo.
>
> I could content myself of the proposed solution :
> @dynamic
> def func (a, b = lambda : []):
> pass
> But I just dislike the fact that the "dynamic" applies to all the defaults,
> even those which weren't supposed to be dynamic (and writing "lambda :
> lambda : []") doesn't look good).
> Would there be any way of separating "to-be-called" lambdas from normal ones
> ? Except with a syntax like "b = dyn(lambda: [])" ?
Use function annotations.
>>> def f(a: dynamic.override=list, b=1): pass
...
>>> f.__annotations__
{'a': dynamic.override}
>>> f.__defaults__
(<class 'list'>, 1)
Just make a decorator that looks in the annotations to figure out what
to replace and what to leave be.
Also "lambda: []" is clearly inferior to "list".
-- Carl
More information about the Python-ideas
mailing list