[Python-ideas] Default arguments in Python - the return - running out of ideas but...

Terry Reedy tjreedy at udel.edu
Thu May 14 01:02:08 CEST 2009


Steven D'Aprano wrote:
> On Thu, 14 May 2009 05:18:37 am CTO wrote:
> 
>> If you thought not reevaluating function expressions
>> was confusing for newbies, wait until you see what making up a new
>> kind of yield will do for them.
>>
>> 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.
> 
> Even if a decorator solution can be made to work, it seems to me that 
> the difficulty with a decorator solution is that it is 
> all-or-nothing -- you can decorate the entire parameter list, or none 
> of the parameters, but not some of the parameters. You can bet that 
> people will say they want delayed evaluation of some default arguments 
> and compile-time evaluation of others, in the same function definition.

Not all or nothing, and selection is easy.  A decorator could only call 
callable objects, and could/should be limited to calling function 
objects or even function objects named '<lambda>'.  And if one wanted 
the resulting value to such a function, escape the default lambda 
expression with lambda.

x=[1,2]
@call_lambdas
def f(a=len(x), lst = lambda:[], func = lambda: lambda x: 2*x):
   # a is int 2, lst is a fresh list, func is a one-parameter function

Terry Jan Reedy




More information about the Python-ideas mailing list