Theoretical question about Lambda

Bengt Richter bokr at oz.net
Thu May 2 16:01:35 EDT 2002


On Thu, 02 May 2002 17:00:11 GMT, Alex Martelli <aleax at aleax.it> wrote:

>Bengt Richter wrote:
>        ...
>>>> Is there any forced need for Lambda's in Python ? I mean does Lambda
>>>> contribute anything that cannot be done otherwise in Python ?
>>>
>>>Not really.  lambda lets you have an anonymous function, and create
>>>a function within an expression (while def is a statement and does
>> A thought: being ble to do it in an expression allows taking advantage
>> of short-circuit evaluation to avoid some problem in a lambda def form
>> vs another.
>
>...while doing it with a def would let you much more easily with
>an appropriate if/else:
>
>if doitoneway:
>    def foo(): return whatever
>else:
>    def foo(): return somethingelse
>
What if doitoneway varies during expression evaluation involving foo,
and whatever and somethingelse involve function calls with side effects?
Would you pass all the unevaluated pieces to an outside function? I'm sure
it would be cleaner sometimes, but I'm not sure about all the time.
C'mon, you can argue both sides ;-)

>> What do you think re short circuit evaluation vav lambda?
>
>Given the lack of a ternary operator in Python (insert appropriate
>smiley fraction here), if/else is much more comfortable for this.
>
ISTM a real ternary or cond expression would be a good addition to Python.

Regards,
Bengt Richter



More information about the Python-list mailing list