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

Terry Reedy tjreedy at udel.edu
Wed May 20 01:01:25 CEST 2009


Oleg Broytmann wrote:
> On Tue, May 19, 2009 at 10:31:00PM +0200, Pascal Chambon wrote:
>> 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 

To repeat: I think one-usage default function objects defined by lambda 
are rather rare. The most obvious is
def ident(ob): return ob
which, if used once, would likely be used more than once and defined as 
above, and which always could be so defined. I suspect even more rare 
is such defaults used in the same function as a mutable default such as 
[] or {} that needs protecting. And in such rare cases, one could 
either pull the function definition into a def statement or prefix it by 
a second lambda.
>> "lambda : lambda : []") doesn't look good).

and I would not suggest it unless what one wanted was for the default 
arg for each call, after the lambda call, to be 'lambda: []' and not '[]'.
>> Would there be any way of separating "to-be-called" lambdas from normal 
>> ones ? Except with a syntax like "b = dyn(lambda: [])" ?
>> @dynamic('b')
> def func (a, b = lambda : []):
> pass

Or dynamic could have an optional explicit list. But that option would 
be rarely needed, I think.
tjr


More information about the Python-ideas mailing list

AltStyle によって変換されたページ (->オリジナル) /