[Python-ideas] Default arguments in Python - the return - running out of ideas but...
CTO
debatem1 at gmail.com
Wed May 20 05:52:37 CEST 2009
On May 19, 9:04 pm, Carl Johnson <cmjohnson.mailingl... at gmail.com>
wrote:
> 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
Already done, as mentioned further up in this list:
<URL: http://code.activestate.com/recipes/576754/>
Geremy Condra
More information about the Python-ideas
mailing list