Proposed PEP for a Conditional Expression

Christian Tanzer tanzer at swing.co.at
Tue Sep 11 03:26:22 EDT 2001


"Alex Martelli" <aleaxit at yahoo.com> wrote:
> """
> One example: with a trivial class, one can put expressions into format
> directives, like
> 
>     "Found %(number)d error%(if number == 1 : '' else : 's')s" % trivial
> 
> This doesn't look too attractive if used in a one-liner, put could be
> put to very good use for substitution in bigger templates.
> """
> Definitely unattractive here -- and you don't really need to
> change the Python language, adding complication to it, to get
> a similar effect right now.  The __getitem__ method of the
> class of 'trivial' can after all perfectly well define whatever
> syntax it wants (as long as it's free of ')' charachters:-)
> for its 'name' argument.  E.g., assuming it's OK to exclude
> '@' and ':' as well, since ')' has to be excluded anyway:

Sure, that's one of Python's strengths. I just think it's a waste if
everybody implements his own pet class to solve one specific instance
of a general problem like this.

IMHO, conditional expressions add substantial benefit if the syntax is 
readable.

> In fact, I'd prefer a totally different
> approach here, one allowing me to write, e.g.:
> 
> print "%(plur_verb number 'Found') %(plur_noun number 'error')s" % neater
> 
> with the parser in __getitem__ looking for spaces and using
> them, if found, to recognize formatting keywords such as
> 'plur_verb' -- makes for easier parsing AND for vastly easier
> i18n of messages (neater itself would of course use gettext
> as needed on the meaning-carrying words such as 'error' or
> 'Found':-).

Well, I'd use the contents of the interpolated string as key to get 
the translation (via gettext or otherwise). After all, different
languages might have totally different rules how to adapt a sentence
to the context supplied by `neater`.

-- 
Christian Tanzer                                         tanzer at swing.co.at
Glasauergasse 32                                       Tel: +43 1 876 62 36
A-1130 Vienna, Austria                                 Fax: +43 1 877 66 92





More information about the Python-list mailing list