Proposed PEP for a Conditional Expression

Marcin 'Qrczak' Kowalczyk qrczak at knm.org.pl
Sun Sep 9 12:17:47 EDT 2001


Sun, 09 Sep 2001 09:42:49 -0500, Michael Chermside <mcherm at destiny.com> pisze:

> Included in this email is a PEP which I am putting together dealing with 
> the idea of introducing a conditional expression into Python. I would 
> welcome any discussion, suggestions, and/or help, sent either to this 
> newsgroup under this topic, or emailed to me at <python at mcherm.com>.

IMHO very well done!

> I prefer the syntax of SPEC_2, but am certainly open to other ideas.

Me too.

>      SPEC_4: Use "if c: a else: b".
>          The following production would be added to Python syntax:
>              expression: "if" expression ":" expression "else" ":"
>                          expression
>          The famous "dangling-else" problem would NOT arise because
>          the "else:" part would be mandatory.
> 
>          PRO - [might allow "if c1: a elif c2: b else: c"]
>          CON - [elif thing would re-introduce dangling else problem]

It would not introduce dangling else. You can replace 'elif' with
'else: if', it's unambiguous.

You couldn't do that in 'if' statement without introducing an
indentation level, which is why 'elif' exists in the first place.
'if' expression doesn't use indentation so having 'elif' there is
not that important.

The fact that there is always an 'else' makes it possible to
distinguish between 'if' statement and 'if' expression if somebody
cares (there shouldn't be any need for distinguising these). The
'if' statement really looks like two statements, split before 'else',
so there is no overlap in syntax.

-- 
 __("<  Marcin Kowalczyk * qrczak at knm.org.pl http://qrczak.ids.net.pl/
 \__/
  ^^                      SYGNATURA ZASTĘPCZA
QRCZAK



More information about the Python-list mailing list