PEP 308: I liked the original proposal better
Anders Hammarquist
iko at cd.chalmers.se
Mon Feb 24 12:20:41 EST 2003
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
>>>
to add additional magic, let call default to None and say:
if call or call is None and callable(true):
and
if call or call is None and callable(false):
and you only need to set call to False if you want to return
a (non-lazy) callable.
/Anders
--
-- Of course I'm crazy, but that doesn't mean I'm wrong.
Anders Hammarquist | iko at cd.chalmers.se
Physics student, Chalmers University of Technology, | Hem: +46 31 88 48 50
G|teborg, Sweden. RADIO: SM6XMM and N2JGL | Mob: +46 707 27 86 87
More information about the Python-list
mailing list