[Python-Dev] conditional expressions - add parens?

Jim Jewett jimjjewett at gmail.com
Tue Mar 7 16:19:06 CET 2006


On 3/7/06, Paul Moore <p.f.moore at gmail.com> wrote:

> The parentheses around genexps were (AFAICT)
> different - without them, the grammar was ambiguous,
> so some way of disambiguating was needed.

The out-of-order evaluation is a very large change,
because now we have a situation where normal
parsing completes an expression, but needs to avoid
evaluating it, just in case.

Currently, we can write:

    >>> if False:
    >>>     print r
    >>> else:
    >>>     print 6

    6

    >>> r

    Traceback (most recent call last):
       File "<pyshell#14>", line 1, in -toplevel-
          r
    NameError: name 'r' is not defined

In the above example, r doesn't get evaluated because
the if ahead of it says to skip that branch.  But with
conditional expressions, that flow control is changed
from *later* in the program.

I don't think we'll see the equivalent of Intercal Suck
Points anywhere but intentionally obfuscated code,
but I do expect to see:

    >>> side_effect() if condition

In fact, I think the below examples are reasonable uses
that do a better job of expressing intent than the if
statement would.  I just don't like the mental backtrack
they require, and would like some sort of advance
warning.  Parentheses at least tell me "You're not done
yet; keep reading."

    >>> ack(r,r) if r not in cache

    >>> log(message) if error_flag

-jJ


More information about the Python-Dev mailing list