[Tutor] Use of "or" in a lambda expression
boB Stepp
robertvstepp at gmail.com
Sun Apr 5 21:29:32 CEST 2015
On Sun, Apr 5, 2015 at 3:06 AM, Alan Gauld <alan.gauld at btinternet.com> wrote:
> On 05/04/15 04:45, boB Stepp wrote:
>
>>>>> He could have done it in various other ways too:
>>>>>
>>>>> eg.
>>>>> lambda : all(print('Hello lambda world!'), sys.exit() )
>
>
>> Well, now I am curious as to why the "all" form evaluates BOTH
>> elements. Apparently it does not apply the short-circuit logic we have
>> been discussing, or it would stop evaluating after the print statement
>> return. Why is that?
>
>
> Because I didn't really think the example through properly.
> I just grabbed the first thing I could think of that would
> evaluate both functions... As Cameron has shown, a much more
> elegant solution is just to use a tuple as the body of
> the lambda.
Not to worry, Alan! This gave me an opportunity to read up on and
learn more about all() and any(), which I had not encountered
previously. Also, much has been clarified about the details of how
and when things get evaluated for various approaches to these lambda
expressions. Many thanks!
--
boB
More information about the Tutor
mailing list