python syntax for conditional is unfortunate
Aaron "Castironpi" Brady
castironpi at gmail.com
Thu Sep 25 03:57:55 CEST 2008
On Sep 24, 8:40 pm, Asun Friere <afri... at yahoo.co.uk> wrote:
> 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.
It's a different answer if you have 'things is/are'. '%d thing%s %s'%
( ( i, )+ ( 's', 'are' ) if i!= 1 else ( '', 'is' ) ). Or excluding
prepositional phrases and subordinate clauses, '%d thing%s'% ( i, 's
are' if i!= 1 else ' is' ).
More information about the Python-list