[Tutor] Use of "or" in a lambda expression

Alan Gauld alan.gauld at btinternet.com
Sun Apr 5 01:55:16 CEST 2015


On 04/04/15 22:57, boB Stepp wrote:
> On Sat, Apr 4, 2015 at 3:35 PM, Alan Gauld <alan.gauld at btinternet.com> wrote:
>> He could have done it in various other ways too:
>>
>> eg.
>> lambda : all(print('Hello lambda world!'), sys.exit() )
>
> Is this what you meant? Because print will always return False. Or did
> you actually mean:
>
> lambda: any(print('Hello lambda world!'), sys.exit())

any() would be more obvious, but in my interpreter
both any() and all() evaluate both functions before
testing the results. At least they do once you
fix the TypeError : they should be in a list/tuple...

lambda : all([print('Hello lambda world!'), sys.exit()] )

> So this is not unusual for Python. BTW, what are some of the other
> languages where this type of expression might be commonly used?

It's sometimes used in Turbo Pascal/Delphi and also in Perl  (a lot!).
I think I've seen it used in PHP too.

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list