"Updating" lambda functions

Steven Bethard steven.bethard at gmail.com
Fri Sep 17 03:46:12 EDT 2004


Elaine Jackson <elainejackson7355 <at> home.com> writes:
> 
> "Terry Reedy" <tjreedy <at> udel.edu> wrote in message
> news:mailman.3428.1095385637.5135.python-list <at> python.org...
> 
> | I am curious if there is any reason other that habit carried over from
> | other languages to not write the above as
> |
> | def fu(x): return x
> | def fu(x): return fu(x) + 17
> | etc
> 
> In my interpreter (IDLE 1.0 on Windows 98) it causes an infinite regress.

Yes, it will, exactly as the lambda version would have.  (And all the 
solutions suggested to you for the lambda form are equally applicable to the 
def form.)  The question is why use the lambda form when you *do* want to bind 
your function to a name?  Basically,

f = lambda args: body

is equivalent to

def f(args): body

except that the def body is a set of statements (so you have to use 'return'), 
and the lambda body is a single expression.

Not that it's coming any time soon, but Python 3000 is supposed to remove 
lambda functions, so when you really *do* want to bind a function to a name 
(like in your case here), it would probably be a good habit to get into to use 
defs instead.

Steve




More information about the Python-list mailing list