[Python-Dev] Re: Update PEP 292

Fernando Perez fperez528 at yahoo.com
Wed Aug 11 14:06:12 CEST 2004


Barry Warsaw wrote:

> Raymond and I had a chance to talk about PEP 292 on bug day, and we've
> exchanged a few private emails and implementations.  I think we've
> finally knocked everything into shape for inclusion in Python 2.4's next
> alpha.
> 
> In the sandbox you will find the updated implementation, along with
> updated tests and documentation (libstring.tex).
> 
> The updated PEP is at:
> 
> http://www.python.org/peps/pep-0292.html

If I understood things correctly, this proposal will not allow the evaluation of
any kind of expression, including attributes.  With the existing interpolation
system, the following happens:

In [2]: print 'sys.platform is: %(sys.platform)s' % locals()
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)

?

KeyError: sys.platform

I understand why this is the case, but it has always struck me as an unpleasant
aspect of string interpolation, since it often forces me to create throw-away
locals when I want to print data stored as attributes of an object.  It is easy
to end up with code that looks like:

_x,_y,_z = self.x, self.y,self.z  # throwaways for printing

print """\
The coordinates are: 
x  =   %(_x)s
y  =   %(_y)s
z  =   %(_z)s """ % locals()

Simply because trying to do %(self.x)s won't work.  This just feels ugly, IMHO.

I wonder if it would be possible to at least allow this kind of expansion
(without going to the full-blown possibilities of PEP 215) in pep-292.  I know
there are tricks to produce this kind of evaluation already (which I sometimes
use).  But since pep-292 is trying to introduce a standard interpolation
mechanism which is easy to use for the common cases, and object attributes are
such a pervasive part of the language, I hope this addition can be considered.

Best,

f

ps. Forgive me if the above is already allowed; if that is the case, it might be
worth pointing it out in an example in the PEP.



More information about the Python-Dev mailing list