PEP 308: I liked the original proposal better

Dave Brueck dave at pythonapocrypha.com
Mon Feb 24 14:29:57 EST 2003


On Mon, 24 Feb 2003, Anders Hammarquist wrote:

> In article <mailman.1046100510.25922.python-list at python.org>,
> Dave Brueck  <dave at pythonapocrypha.com> wrote:
> >> Short-circuiting and everything else required in one.
> >>
> >> this-was-going-to-be-a-wink-but-I-like-this-idea-ly yours
> >
> >Better put the wink back in. :) Unfortunately, it doesn't provide short
> >circuiting for any of the times when short-circuiting is actually
> >important - it's unlikely that a parameterless function call would be that
> >useful, not to mention the more common uses like preventing invalid
> >attribute access, preventing divide by zero, etc. :(
>
> Sure it does:
> >>> def ifelse(cond, true, false, call=False):
> ...     if cond:
> ...         if call:
> ...             return true()
> ...         else:
> ...             return true
> ...     else:
> ...         if call:
> ...             return false()
> ...         else:
> ...             return false
> ...
> >>> a
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> NameError: name 'a' is not defined
> >>> b = 42
> >>> ifelse('a' in globals(), lambda : a, lambda : b, True)
> 42
> >>>

Wrapping stuff in lambda != ifelse providing short-circuiting. If short
circuiting was working, then I'd be able to do:

timeLeft = ifelse(rate > 0, amountLeft / rate, 'n/a')

Or did you forget smileys too? :)

-Dave





More information about the Python-list mailing list