Re: [Info] PEP 308 accepted - new conditional expressions
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.python archive

Re: [Info] PEP 308 accepted - new conditional expressions

From: Reinhold Birkenfeld <reinhold-birkenfeld-nospam@wolke7.net>
Date: Fri Sep 30 2005 - 23:06:25 CEST

Rocco Moretti wrote:
> Reinhold Birkenfeld wrote:
>> Hi,
>>
>> after Guido's pronouncement yesterday, in one of the next versions of Python
>> there will be a conditional expression with the following syntax:
>>
>> X if C else Y
>
> Any word on chaining?
>
> That is, what would happen with the following constructs:
>
> A if B else C if D else F
> A if B if C else D else F
>
> The first one is the tricky bit - it could be either
>
> (A if B else C) if D else F
> or
> A if B else (C if D else F)
>
> I'd expect the former from left-> right semantics, but reading the
> unparenthesized form, I'd see "A if B else ..." note that B is true, and
> conclude the expression evaluated to A (which would be wrong if D is false).

It will be

A if B else (C if D else F).

Quote:
"""
The priorities will be such that you can write

    x = A if C else B
    x = lambda: A if C else B
    x = A if C else B if D else E

But you'd have to write

    if (A if C else B):
    [x for x in seq if (A if C else B)]
    A if (X if C else Y) else B
    (A if C else B) if D else E

Note that all these are intentionally ugly. :)
"""

Reinhold
Received on Sat Oct 15 04:01:12 2005