[Israel.pm] Interpolation in Python (Sort-of)

Shlomi Fish shlomif at iglu.org.il
Mon Nov 13 06:07:46 PST 2006

In regards to Ran's presentation about quoting in Perl, it turns out Python 
has an interpolation-like feature:

shlomi:~$ python
Python 2.4.3 (#2, Oct  6 2006, 15:32:41)
[GCC 4.1.1 20060724 (prerelease) (4.1.1-3mdk)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> "Hello %(name)s, it's %(hour)02d:%(min)02d now." % 
{ 'name':"Haim",'hour':16,'min':5 }
"Hello Haim, it's 16:05 now."

These are called named (sprintf) conversions and are a feature of Python's "%" 
operator that implements sprintf-like functionality. One difference from Perl 
is that you need to use it with a hash (or dicitionary in Python's 
terminology). I wonder if you can pass it the symbol table, to get a more 
Perl like "Hello $name!" behaviour. And naturally, it doesn't support more 
complex expressions than simple parameters.

I was looking for a feature like that for a Test::Run plugin I'm writing now. 
(For its mini-template system). Searching for "named conversions" in Google 
yielded a changelog that implied that Python has them, and this page 
confirmed this:


Since I couldn't find anything of relevance on CPAN, and the people on 
Freenode's #perl said they weren't aware of anything like that, I guess I'm 
going to write an implementatoin of this for Perl.


	Shlomi Fish

Shlomi Fish      shlomif at iglu.org.il
Homepage:        http://www.shlomifish.org/

Chuck Norris wrote a complete Perl 6 implementation in a day but then
destroyed all evidence with his bare hands, so no one will know his secrets.

More information about the Perl mailing list