[Python-ideas] Default arguments in Python - the return - running out of ideas but...
Arnaud Delobelle
arnodel at googlemail.com
Wed May 13 21:44:14 CEST 2009
On 13 May 2009, at 20:18, CTO wrote:
> Why not just push for some decorators that do this to be included in
> stdlib? I see the utility, but not the point of adding extra syntax.
>
>>>> @Runtime
> ... def f(x=a**2+2b+c):
> ... return x
> ...
>>>> a = 1
>>>> b = 2
>>>> c = 3
>>>> f()
> 8
>
> This seems much more intuitive and useful to me than adding new
> meanings to yield.
This is not possible.
def f(x=a**2+2*b+c):
return x
is compiled to something very much like:
_tmp = x**2+2*b+c
def f(x=_tmp):
return x
So it is impossible to find out what expression yields the default
value of x by just looking at f. You have to use lambda or use George
Sakkis' idea of using strings for defaults and evaluating them at call-
time (but I'm not sure this will work reliably with nested functions).
--
Arnaud
More information about the Python-ideas
mailing list