[Patches] [ python-Patches-471421 ] conditional expression (if-then-else)

noreply@sourceforge.net noreply@sourceforge.net
Thu, 18 Oct 2001 11:48:29 -0700


Patches item #471421, was opened at 2001-10-15 11:59
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=305470&aid=471421&group_id=5470

Category: Core (C code)
Group: None
>Status: Closed
>Resolution: Rejected
Priority: 3
Submitted By: Guido van Rossum (gvanrossum)
Assigned to: Guido van Rossum (gvanrossum)
Summary: conditional expression (if-then-else)

Initial Comment:
Here's an implementation of conditional expressions of
the form

    if <test> then <test> else <test>

It's hairier than expected because I'm trying to
require you to put parentheses around it in some cases
but not in others.

This still lacking:

- a PEP to motivate and explain it
- documentation
- tests
- needed changes to Modules/parsermodule.c

After applying this patch, you must regenerate the
grammar; the Unix Makefile and the current CVS version
of the Windows project file do this automatically.

----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2001-10-16 13:09

Message:
Logged In: YES 
user_id=6380

The more I think about this, the less I like it -- mostly
because I cannot find decent examples in the std library
that would benefit from this...

----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2001-10-15 14:01

Message:
Logged In: YES 
user_id=6380

OK, conditional-2.txt is a patch that includes the
paren-less "else if", as well as the graminit.[ch] diffs.

----------------------------------------------------------------------

Comment By: Tim Peters (tim_one)
Date: 2001-10-15 13:50

Message:
Logged In: YES 
user_id=31435

About elif, I channeled you as leaving it out deliberately 
<wink>.  A long chain of these is probably crying out for a 
dict lookup instead:

p = {1: 'one', 2: 'two', 3: 'three'}.get(x, 'many')

where the dict literal wouldn't really be written inline.

Short of adding elif, though (OK by me), the "extra" parens 
needed using "else if" instead must go.

----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2001-10-15 13:04

Message:
Logged In: YES 
user_id=6380

Somthing to think about: should this support 'elif'?  Like
this:

  p = if x==1 then 'one' elif x==2 then 'two' elif x==3 then
'three' else 'many'

With the current patch that would have to be written using
ugly extra parentheses:

  p = if x==1 then '1' else (if x==2 then '2' else (if x==3
then '3' else '*'))

It would be easy enough to allow these parentheses to be
omitted: just change the conditional to 'if' test 'then'
test 'else' (test | conditional).

Allowing 'elif' might then not be needed.


----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2001-10-15 12:47

Message:
Logged In: YES 
user_id=6380

The 'fi' would disambiguate binary operators following the
else part. But I find it very unpythonic: we don't have
Algol-68 style reversed closing keywords anywhere else, and
when you squint you don't see the structure.

----------------------------------------------------------------------

Comment By: Tim Peters (tim_one)
Date: 2001-10-15 12:44

Message:
Logged In: YES 
user_id=31435

Martin, I don't think so.  "The problem" is ensuring the 
parser can distinguish a conditional expression from an if 
statement, as they both begin with "if".  A left paren 
rules out an if-statement.  If the conditional expression 
*began* with "fi", no problem <wink>.

----------------------------------------------------------------------

Comment By: Martin v. Löwis (loewis)
Date: 2001-10-15 12:34

Message:
Logged In: YES 
user_id=21627

Would the grammar become simpler if the syntax was

if <test> then <test> else <test> fi

?


----------------------------------------------------------------------

Comment By: Tim Peters (tim_one)
Date: 2001-10-15 12:22

Message:
Logged In: YES 
user_id=31435

FYI, the grammar is not regenerated by magic on Windows.  I 
very recently checked in Parser/grammar.mak (a Windows 
nmake file), which you can use "by hand" (read the comments 
at the top of the file) to regenerate the grammar if you 
know that's needed.

----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=305470&aid=471421&group_id=5470