Update to PEP308: if-then-else expression

Rich Harkins rich at worldsinfinite.com
Tue Feb 11 16:04:20 CET 2003


I posted this elsewhere under a different subject and got zero responses
of any type.  Perhaps if I post it to this thread I can get indications
of why this would be a bad idea (some slight revisions have been made
but the idea is the same):


I suggest creating a new singleton called "Default" or some such which
has the following properties:

1) When used as the left-hand side of a logical "and", the right hand
side is not evaluated and the result is Default.
2) Always acts as a false in other boolean operations (such as "or").
3) When used as a function: if the argument passed is non-zero (True)
then return the argument otherwise return the Default object.
4) Overload the keyword "else" (default OR) to act exactly as
traditional "or" except that it will ONLY execute the right hand side if
the left hand side is Default (thus eliminating the false-left-side
case).

For example:

Default(condition) and truecase else falsecase

One could very well alias "Default" as "Test" if 7 letters is too much
typing (i.e.: Test(condition) and truecase else falsecase).

By doing this truecase could produce any value and the falsecase is
never executed.  In addition, truecase COULD produce Default indicating
that it the falsecase value is acceptible to the truecase after all.  As
an added bonus, the following also makes sense:

x=Default               # We don't care
print "x is",x else 5   # 5 because x is Default.
x=0
print "x is now",x else 5 # 0 because x is not Default

Obviously, the spellings would have to change as appropriate but you get
the idea.

To sum up, why couldn't we create an entity such that it yields to any
other entity when manipulated?

Here are the advantages I see to this:

1) No new keywords and only alters the meaning of one keyword.
2) Does not require trinary operation and looks like the classic and-or
pattern.
3) Visible enough to indicate something interesting is happening.
4) Creates a useful new value that can yield to any other value
including false values (unlike None as it stands now).
5) Maintains short-circuiting properties.


Anyway, since I didn't get a response to my previous posting I thought
I'd run it up the flagpole one more time and check to see if anyone even
reading it.

Rich







More information about the Python-list mailing list