better lambda support in the future?

Steven Bethard steven.bethard at gmail.com
Fri Dec 17 16:03:26 EST 2004


Fredrik Lundh wrote:
> Steven Bethard wrote:
> 
> 
>>Even if you could settle the syntax issue, once you've decided that you really do need a true 
>>block in an anonymous function, you're not really saving much space by not declaring it:
>>
>>def f(*args):
>>    # body line 1
>>    # body line 2
>>    # ...
>>    # body line N
>>x = func or f
>>
>>v.s.
>>
>>x = func or lambda *args:
>>                # body line 1
>>                # body line 2
>>                # ...
>>                # body line N
> 
> 
> you meant:
> 
>     def x(*args):
>         # body line 1
>         # body line 2
>         # ...
>         # body line N
> 
>     v.s.
> 
>     x = func or lambda *args:
>         # body line 1
>         # body line 2
>         # ...
>         # body line N
> 
> right?

You're welcome to name the function whatever you want -- notice in my 
example that the function is used in the statement:

x = func or f

If you'd prefer the statement to read:

x = func or x

that's also fine.  Depends on what exactly 'x' is, and whether or not it 
really makes sense for the function I called 'f' to have the same name 
as the variable called 'x'.  It certainly may, but since I wasn't giving 
real code, I didn't want to commit to that.

Perhaps a better example would have been something along the lines of:

dict(a=lambda *args:
            # body 1
      ,
      b=lambda *args:
            # body 2
      ,
      c=lambda *args:
            # body 3
)[s](values)

v.s.

def a_func(*args):
     # body 1
def b_func(*args):
     # body 2
def c_func(*args):
     # body 3
dict(a=a_func, b=b_func, c=c_func)[s](values)

where it's clear that I'm trying to use lambdas where expressions are 
required.

I assume that the point you were trying to make is that:

def f(*args):
     return expr

is equivalent to

f = lambda *args: expr

?

Steve



More information about the Python-list mailing list