python syntax for conditional is unfortunate

Asun Friere afriere at yahoo.co.uk
Wed Sep 24 21:40:12 EDT 2008


On Sep 25, 3:16 am, Pete Forman <pete.for... at westerngeco.com> wrote:
> Asun Friere <afri... at yahoo.co.uk> writes:
>
>  > A canonical use of the conditional operator is in
>  > pluralising words, (eg. '%s dollar' % n + 's' if n!=1 else '').
>
> That fails for n == 1.  So what is best?
>

Sorry missing parantheses.  I should test, even for fragments written
out as part of a sentence. %-/

> for i in range(4):
>     print '%d thing' % i + ('s' if i != 1 else '')
>

That's the corrected version of what I meant, but actually I think
your last version ('%d thing%s' % (i, 's' if i != 1 else '')), holding
all variables for placeholders in the tuple, is better. It's certainly
more readible.



More information about the Python-list mailing list