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

Bruce Leban bruce at leapyear.org
Thu May 14 04:41:17 CEST 2009


On Wed, May 13, 2009 at 5:10 PM, George Sakkis <george.sakkis at gmail.com>wrote:

> On Wed, May 13, 2009 at 7:08 PM, Terry Reedy <tjreedy at udel.edu> wrote:
>
> > Bruce Leban wrote:
> >> def myfunc(a, b, c = *lambda: expression):
> >>  stuff
>
> > 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.
>
> 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).
>

I think similar syntax should do similar things. If *arg means one thing and
&arg means something else, that's confusing. There are lots of non confusing
alternatives:

   def foo(a, b := lambda: bar):
   def foo(a, b = & lambda: bar):
   def foo(a, @dynamic b = lambda: bar):   # adding decorators on parameters

   and more

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: []):
>

Explicit is better than implicit. There's a thunk getting created here,
right? Don't you want that to be obvious? I do.

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.


It's not free and that adds quite a bit of complexity. Note that default
parameters are evaluated in the context of where the function is defined,
NOT in the middle of setting the other function parameters. (That's true for
this new case too.)

Sure Lisp has let and let* but the proposal here is NOT to provide arbitrary
computational ability in the parameter list but to provide a way to have
defaults that are not static. We shouldn't over-engineer things just because
we can.

--- Bruce
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20090513/8e669aa7/attachment.html>


More information about the Python-ideas mailing list