Variable Interpolation - status of PEP 215

Peter Hansen peter at engcorp.com
Thu Jun 20 00:09:46 EDT 2002


Ganesan R wrote:
> 
> I am a relative newbie to Python. I have a reasonably good perl background
> (don't worry I am not about to start a language flame war :-), and one of
> the things I really miss is variable interpolation in strings. I searched
> the net and found PEP 215 (http://www.python.org/peps/pep-0215.html). This
> seems to be exactly what I need. The proposal seems to be quite old, so I'd
> like to know if it's still in consideration or has it been dropped
> completely?

Evading the question: are you aware of the ability of the Python 
formatting operator to retrieve items from a dictionary rather 
than just by order in a sequence?

>>> foo = "trick"
>>> bar = 95.5
>>> print "This %(foo)s is loved by %(bar)s%% of programmers." % locals()
This trick is loved by 95.5% of programmers.

You can use any dictionary, but locals() or globals() can be handy
in many cases.

A search of the archives for the list/newsgroup should also turn up
a handful of implementations that pretty much do what the Perl 
version does with a dozen lines of Python code as setup.

-Peter



More information about the Python-list mailing list