For review: PEP 308 - If-then-else expression

holger krekel pyth at devel.trillke.net
Sun Feb 9 23:01:34 EST 2003


Andrew Koenig wrote:
> holger> Maybe Roman Suzi (IIRC) has found one (short-circuiting)
> 
> holger>     cond -> (true_expr, false_expr)
> 
> holger> which i am currently thinking about.  
> 
> I'm not thrilled about this example because if you cover up the
> "cond ->" part, you get what looks like a tuple, and tuples don't
> short-circuit.
> 
> I'd be happy with
> 
>         true_expr if cond else false_expr
> 
> but must confess that I'm growing more in favor of
> 
>         if cond: true_expr else: false_expr
> 
> even though it looks like an if statement -- or perhaps *because*
> it looks like an if statement.
> 
> I don't think there's any real ambiguity from the compiler's
> viewpoint, because "if" denotes a statement only when it is the first
> token of the statement and an expressin otherwise.

But 

    <expr>

is a perfectly valid statement. e.g.

    somelist.append(1)

so

    if something: 

would leave the parser having to look ahead of what this means.

    holger





More information about the Python-list mailing list