Proposed PEP for a Conditional Expression

Gareth McCaughan Gareth.McCaughan at pobox.com
Tue Sep 11 18:56:06 EDT 2001


Terry Reedy wrote:

[Christian Tanzer:]
> > 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.

class Trivial:
  def __getitem__(self, key):
    return eval(key, globals(), locals())

"foo %(1+2+3)s bar" % Trivial()    ===> "foo 6 bar"

Giving it access to the caller's local variables
isn't quite so easy.

-- 
Gareth McCaughan  Gareth.McCaughan at pobox.com
.sig under construc



More information about the Python-list mailing list