compound statement from C "<test>?<true-val>:<false-val>"

Gabriel Genellina gagsl-py at yahoo.com.ar
Sun Feb 11 21:11:37 EST 2007


En Sun, 11 Feb 2007 19:16:49 -0300, Holger <ishoej at gmail.com> escribió:

>> >> if n == 1:
>> >>     print "I saw a car"
>> >> else:
>> >>     print "I saw %d cars" % n
>
> Personally I don't like the if-else approach because of the don't-
> repeat-yourself philosophy
>
>> D'accord. Did I mention that, as a "for fun" approach, "s" * (n != 1) is
>> quite clever :-)
> I like this one :-)
>
>> print "I saw %d car%s\n" % (n, ("", "s")[n != 1])
> And this one.

I presume all of this is only used as an example on using expressions. In  
any application with any chances of being i18n, the only viable way is the  
first one. Doing algebra on phrases is a no-no.

-- 
Gabriel Genellina




More information about the Python-list mailing list