[Python-Dev] conditional expressions - add parens?

Tim Peters tim.peters at gmail.com
Tue Mar 7 05:47:06 CET 2006


[Jim Jewett]
>>> I think that adding parentheses would help, by at least signalling that
>>> the logic is longer than just the next (single) expression.
>>>
>>>     level = (0 if "absolute_import" in self.futures else -1)

[Steve Holden]
>> Contrast with the bleeding obvious:
>>
>>     level = 0
>>     if "absolute_import" in self.futures:
>>         level = -1
>>
>> or even, if a certain obscurity is desirable:
>>
>>     level = - ("absolute_import" in self.futures)

In honor of Peter Naur receiving the 2005 Turing Award:

<http://campus.acm.org/public/pressroom/press_releases/3_2006/turing_3_01_2006.cfm>

and remembering Python's Algol roots, I'd like to constrast it with
the truly obvious:

    level = (if "absolute_import" in self.futures then 0 else -1)

That way also has the minor advantage of computing the same value for
`level` as Jim's code ;-)

[Joe Smith]
> Wait a second.
>
> I may be just a user but if the above is correct then that syntax needs to
> die!
> There is no logical reason for "XX if YY else ZZ" to be roughly equivlent
> to:
> "if (YY) then {ZZ} else {XX}" , but AFAICT that is pretty much the way you
> expanded that.

Ya, Steve just got it backwards.  "(X if Y else Z)" is the same as
"(if Y then X else Z)", except for the excessive novelty.  The obvious
spelling would require making "then" a keyword, which is actually OK
with everyone :-)

> I hope I misunderstood, or that there was a typo in a post.

You were lucky this time, bucko, but don't ever go questioning a
python-dev regular again ;-)


More information about the Python-Dev mailing list