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

MRAB google at mrabarnett.plus.com
Wed May 13 16:28:48 CEST 2009


Jacob Holm wrote:
> Pascal Chambon wrote:
>> One last idea I might have : what about something like
>>
>> *    def myfunc(a, b, c = yield []):
>>               pass*
>>
> 
>> [...], but there is no interpretation conflict for the parser, and we 
>> might quickly get used to it
> 
> 
> I am surprised that there is no conflict, but it looks like you are 
> technically right.  The parentheses around the yield expression are 
> required in the following (valid) code:
> 
> 
>  >>> def gen():
> ...     def func(arg=(yield 'starting')):
> ...         return arg
> ...     yield func
> ...
>  >>> g = gen()
>  >>> g.next()
> 'starting'
>  >>> f = g.send(42)
>  >>> f()
> 42
> 
> 
> I would hate to see the meaning of the above change depending on whether 
> the parentheses around the yield expression were there or not, so -1 on 
> using "yield" for this.
> 
> I'm +0 on the general idea of adding a keyword for delayed evaluation of 
> default argument expressions.
> 
There's the suggestion that Carl Johnson gave:

     def myfunc(a, b, c else []):
         pass

or there's:

     def myfunc(a, b, c def []):
         pass

where 'def' stands for 'default' (or "defaults to").



More information about the Python-ideas mailing list