substitute for c/java's ?:

Alex Martelli aleaxit at yahoo.com
Thu Jun 14 12:29:38 EDT 2001


"Jochen Riekhof" <jochen at riekhof.de> wrote in message
news:9galsa$ssh$07$1 at news.t-online.com...
> >return ("a","an")[name[0].lower() in "aeiou"]
>
> Yeah, this is the method I found out for myself (see one of my above posts
> in this thread).
> Still has the drawback to evaluate both values of choice and much less
> readable than ?: IMO.

For this example case, "evaluating" both values is absolutely
no drawback -- such "evaluation" being just about no-cost here.

As for comparing this to a hypothetical

   return (name[0].lower() in "aeiou")?"an":"a"

I personally think they're both so absurdly unreadable that trying
to deem either "more readable" border on the ridiculous.  On a
scale of 0 (Perl) to 100 (idiomatic Python), I would call the
former a 0.5 and the latter a 0.3 -- at least the former shows
immediately between WHAT values the choice is going to happen,
and only later the vastly-more-complex choice-condition.


Alex







More information about the Python-list mailing list