substitute for c/java's ?:

Marcin 'Qrczak' Kowalczyk qrczak at knm.org.pl
Mon Jun 18 15:57:24 EDT 2001


Thu, 14 Jun 2001 04:20:30 -0500 (CDT), Glyph Lefkowitz <glyph at twistedmatrix.com> pisze:

>> The ?: operator is overrated. For the time you save typing, you
>> are wasting someone else's because they need to figure out what you were
>> thinking.
> 
> Trust me, this is true.  Compare the following actual examples:

You are cheating: the real difference was between ||'ed comparisons
and in, not between ?: and if-statement.

> def aan(name):
>     """Utility which returns 'a' or 'an' for a given noun.
>     """
>     if string.lower(name[0]) in ('a','e','i','o','u'):
>         return 'an '
>     else:
>         return 'a '

def aan(name):
    """Utility which returns 'a' or 'an' for a given noun.
    """
    return if string.lower(name[0]) in 'aeiou' then 'an ' else 'a '

Is it really that bad?

Now I cheated by using a string instead of a tuple :-)

BTW, results are wrong for aan('university'), aan('hour'), aan('XMS').

-- 
 __("<  Marcin Kowalczyk * qrczak at knm.org.pl http://qrczak.ids.net.pl/
 \__/
  ^^                      SYGNATURA ZASTĘPCZA
QRCZAK



More information about the Python-list mailing list