Proposed PEP for a Conditional Expression

Peter Mayne Peter.Mayne at au1.ibm.com
Wed Sep 12 05:10:33 EDT 2001


An idea borrowed from BLISS...

In BLISS, all statements are expressions, to the point that BEGIN and END
are interchangeable with ( and ).

The assignment statement
  x = 1
has the value 1, as does the expression statement
  1

Simple statements such as "print" return None.

Compound statements have the obvious value:

value =
  if x>3:
    4
  else:
    5

This if statement has the value 4 or 5, depending on the value of x.

Now we get complicated.

sum = 0
i = 0
total =
  while 1:
    if i==10:
      break sum
    sum += i
    i += 1

The while statement has the value of the last statement executed, which in
this case is "break sum". Consequently, "total" has the value of the while
statement.

An equivalent to the above is

total =
  sum = 0
  for i in range(10):
    sum += i

Since "sum += i" is the last statement executed, total has the final value
of sum.

If statements and expressions are the same thing, then there's no need to
worry about special syntax for conditional expressions, because the language
can already handle such things by definition.

PJDM
--
Peter Mayne
IBM GSA (but the opinions are mine)
Canberra, ACT, Australia
This post is covered by Sturgeon's Law.





More information about the Python-list mailing list