Theoretical question about Lambda

Terry Reedy tjreedy at udel.edu
Thu May 2 14:11:40 EDT 2002


"Alex Martelli" <aleax at aleax.it> wrote in message
news:vgeA8.40079$8D3.1169254 at news1.tin.it...
> Bengt Richter wrote:
> > 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 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.

This is a matter of taste, not objectivity.  Given that bool(function)
== True for every function, the and/or ternary construction -- which
Python *does* have -- is, for short expressions, easy, quicker to
write, and slightly safer (due to not redundantly typing the function
name twice, as with +=, etc.):

foo = doitoneway and (lambda: whatever) or (lambda: somethingelse)

It saves screen/page lines and to me is easier to grasp.

Terry J. Reedy






More information about the Python-list mailing list