"Updating" lambda functions
Bengt Richter
bokr at oz.net
Fri Sep 17 04:26:19 EDT 2004
On Fri, 17 Sep 2004 07:46:12 +0000 (UTC), Steven Bethard <steven.bethard at gmail.com> wrote:
>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.
Yes, but
obj.f = lambda args: body
is possible without an intermediate local binding of f that might clobber a previous f, as in
def f(args): body
obj.f = f
del f # if you want to clean up. But better save the old f in that case?
I'd rather use lambda.
>
>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.
>
Well, if lambda is removed (or not ;-), I hope an anonymous def expression is allowed,
so we can write
obj.f = def(args):
body
or
obj.f = def(args): body
or
obj.f = (
def(args):
body
) # at or to the left of the def column, for final dedent without special ')' processing.
(where def body indentation is referenced from wherever the def is)
Regards,
Bengt Richter
More information about the Python-list
mailing list