(test?return_true:return_false)

Sean Ross frobozz_electric at hotmail.com
Sat Jan 25 00:14:53 CET 2003


"Robin Munn" <rmunn at pobox.com> wrote in message
news:slrnb32738.5jj.rmunn at localhost.localdomain...
[snip]
> So for the example you quoted:
>
>     print 'Search complete (%d match%s
found)'%(matches,(matches==1?'':'es'))
>
> the "a and b or c" trick won't work, since an empty string is considered
> false. You would need to invert the test. Instead of:
>
>     (matches==1) and '' or 'es'
>
> use:
>
>     (matches!=1) and 'es' or ''
>
> This will work properly.

Or, from the "Python Cookbook":
    (matches == 1 and [''] or ['es'])[0]
or
    ('', 'es')[matches != 1]
or, for plurals in particular,
    's'*(matches != 1)







More information about the Python-list mailing list