substitute for c/java's ?:

Jochen Riekhof jochen at
Tue Jun 12 12:18:47 EDT 2001


as a newcomer to python (like it a lot!) I found almost everything great
except two things:
1. (most important)
I am missing something like the c/Java ?: operator. This is so convenient in
many places and saves a lot of typing.
It works like
result = option ? value1 : value2;
which is equivalent to
if option:
    result = value1
    resultl = value2

Is there anything I overlooked?

2. switch (not important)
if elif else is not a proper substitute for switches, as the variable in
question has to be retyped in each if/elif clause.
One can shorten the code by assigning to a temporary variable, but I
consider switch more elegant. The ability
to specify limits like 0 <=x <=10 is cool, however.

Otherwise, one of the coolest languages I learned so far!!



Jochen Riekhof
jochen at
Independent Software Developer

More information about the Python-list mailing list