[Python-Dev] vox populii illiterati
Ka-Ping Yee
ping@zesty.ca
Mon, 10 Feb 2003 02:17:29 -0600 (CST)
On Mon, 10 Feb 2003, Alex Martelli wrote:
> Forms such as:
>
> [ f(23) for f in stuff if callable(f) ]
>
> have been in Python for years now, and you could indeed
> say "the very first thing you see" (the call f(23)) is "getting
> short-circuited" by an if-guard at the very end.
As someone else said, i do consider this out-of-order evaluation
a drawback of list comprehensions, but the confusion is not so
bad because the entire construct is enclosed in brackets.
So at least the whole thing begins with a signal and has a
distinctive shape to the reader.
> syntax similar to list comprehensions (but with
> mandatory if and else clauses and no for clause, instead
> of a mandatory for clause and optional further clauses),
>
> [ f(23) if callable(f) else None ]
Requiring the brackets here helps somewhat to signal grouping,
but the overload isn't really appropriate because there are no
collections involved.
The other difference is that, in your first example above,
every element of the result really will have the form f(23);
the extra clauses only select which elements appear.
In the second example, the result might have no relationship
at all to f(23). The nature of the short-circuiting is quite
different -- so much so that i think it's quite a stretch to
refer to the first example as short-circuiting.
-- ?!ng