Nifty interpolation hack [was Re: Equivalent 2->> print '%d %d' %(l[i], for i in range(2))?]
Andrew Dalke
dalke at acm.org
Thu May 17 22:30:48 EDT 2001
Nick Mathewson wrote:
>On 18 May 2001 00:04:30 +0200, Carel Fellinger <cfelling at iae.nl> wrote:
>>>>> class C:
>>... def __init__(self, lst):
>>... self.lst = lst
>>... def __getitem__(self,key):
>>... return self.lst[eval(key)]
>>...
>>>>> l=[1,2,3,4,5,6,7,8,9]
>>>>> "%(1)d %(2+3)d" % C(l)
>>'2 6'
>
>Holy crud! This _works_!? How come I've never seen this before?
>It's such a beautiful kluge enabler! (Has anybody done this before? I
>should submit it to that cookbook thingie.)
It's come up before on c.l.py. See
http://mail.python.org/pipermail/python-list/2000-December/023151.html
for an example I mentioned where I used this for a doc generator,
by using the text inside the %() as a set of transformation operations.
I am still astonished that it works this way - happy, but astonished.
Andrew
dalke at acm.org
More information about the Python-list
mailing list