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

George Sakkis george.sakkis at gmail.com
Thu May 14 02:10:42 CEST 2009


On Wed, May 13, 2009 at 7:08 PM, Terry Reedy <tjreedy at udel.edu> wrote:

> Bruce Leban wrote:
>>
>> Here's what I'd like:
>>
>> def myfunc(a, b, c = *lambda: expression):
>>  stuff
>>
>> The use of the lambda keyword here makes the scope of any variables in the
>> expression clear. The use of the prefix * makes the syntax invalid today,
>> suggests dereferencing and doesn't hide the overhead. This is equivalent to:
>
> There is a proposal, which I thought was accepted in principle, to make '*
> seq' valid generally, not just in arg-lists. to mean 'unpack the sequence'.
> * (lambda:1,2)() would then be valid, and without the call, would be a
> runtime, not syntax error.
>
> Other than that ;0(, it would be an interesting idea.

Then how about putting the * before the parameter ?

    def myfunc(a, b, *c = lambda: expression):

It's currently a syntax error, although the fact that "*arg" and
"*arg=default" would mean something completely different is
problematic. Still the same idea can be applied for some other
operator (either valid already or not).

Regardless of the actual operator, I came up with the following
additional subproposals.

Subproposal (1): Get rid of the explicit lambda for dynamic arguments. That is,

    def myfunc(a, b, *x=[]):

would be equivalent to what previous proposals would write as

    def myfunc(a, b, *x=lambda: []):

Subproposal (2): If subproposal (1) is accepted, we could get for free
(in terms of syntax at least) dynamic args depending on previous ones.
That is,

    def myfunc(a, b, *m=(a+b)/2):

would mean

    def myfunc(a, b, *m = lambda a,b: (a+b)/2):

with the lambda being passed the values of a and b at runtime.

Thoughts ?

George



More information about the Python-ideas mailing list