Trinary operator?

Andrew Dalke dalke at dalkescientific.com
Wed Apr 17 14:20:15 EDT 2002


Philipp Lenssen:
>can I use something like
>    verboseGender = (gender == 'm') ? 'male' : 'female'
>in Python?

No.  Search the archives for "ternary operator" for plenty of
past discussion.  Google lists 65 posts for comp.lang.python.

Besides, for this specific case you should have some error checking,
because there are genders other than 'male' and 'female' (eg,
asexual bacteria and hermaphradite earthworms).

  verboseGenderTable = {
     "m": "male",
     "f": "female",
  }
  verboseGender = verboseGenderTable[gender]

Then if something other than 'm' or 'f' is used, this will raise
a KeyError.

                Andrew
                dalke at dalkescientific.com






More information about the Python-list mailing list