Variable Interpolation - status of PEP 215

Andrew McNamara andrewm at object-craft.com.au
Thu Jun 20 00:40:38 EDT 2002


>> 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?
[...]
>>>> 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.

It took me a while to come to love the % operator - it really is a
neat idea. It means that you don't have to worry about escaping the
interpolation characters unless you explicitly want interpolation. In
perl, I seemed to be forever forgetting to escape them, producing obscure
bugs as a result. And it doesn't require any special rules in the grammer - 
it's just another operator.

And in C, the problem is even worse (although it's not a fair comparison) - 
you have to allocate the memory first, worry about bounds checking,
call sprintf (really snprintf), then worry about releasing the memory.

-- 
Andrew McNamara, Senior Developer, Object Craft
http://www.object-craft.com.au/





More information about the Python-list mailing list