Proposed PEP for a Conditional Expression

Christian Tanzer tanzer at swing.co.at
Tue Sep 11 03:30:50 EDT 2001


"Terry Reedy" <tjreedy at home.com> wrote:

> "Christian Tanzer" <tanzer at swing.co.at> wrote in message
> news:mailman.1000105331.31775.python-list at python.org...
> 
> >Conditional expressions provide some nice benefits totally unrelated to lambdas.
> > 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
> 
> Python does not evaluate expressions inside strings and I do not
> expect it ever will.

It does already. All you need is to write a class defining an
appropriate `__getitem__` method and pass an instance thereof to the
`%` operator applied to the string in question.

> However, this can be rewritten today as
> 
>  "Found %(number)d error%s" % (num, num != 1 and 's' or '')

How is it that people complain that conditional expressions make for
unreadable code and then advocate obfuscations like the one above?
Besides, this style fares very badly if you ever want to go for I18N.

-- 
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