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: Ron Adam <rrr@ronadam.com>
Date: Sat Oct 01 2005 - 00:33:03 CEST

Reinhold Birkenfeld wrote:

> 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)

So this evaluates as if there are parentheses around each section.. Hmm?

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

The first 'if' divided the expr, then each succeeding 'if' divides the
sub expressions, etc... ?

So ...

   A if B else C + X * Y

Would evaluate as... ?

   A if B else (C + X * Y)

and...

    value = X * Y + A if B else C

would be ?

    value = (X * Y + A) if B else C

or ?

    value = X * Y + (A if B else C)

I think I'm going to make it a habit to put parentheses around these
things just as if they were required.

Cheers,
Ron
Received on Sat Oct 15 04:01:25 2005