Draft PEP: string interpolation with backquotes

Fernando Pérez fperez528 at yahoo.com
Sun Dec 2 16:17:12 EST 2001


phil hunt wrote:

>>
>>than *any* of the existing alternatives in python. Here it is:
>>
>>In [23]: itpl(" Pos: ($i,$j,$k), Dist = $sqrt(i**2+j**2+k**2), \
>>   ....: Corr = $C[i][j][k]")
>>Out[23]= ' Pos: (1,2,3), Dist = 3.74165738677, Corr = 5'
>>
>>I read that line and I can know exactly what it's doing, what it
>>evaluates and what data it will show. And I can dig out examples
>>where things are 100 times more complicated than this.
> 
> I have *no problem* with this at all. It doesn't involve extending
> the syntax of python, and incidently demonstrates that Python
> already does what you want.

I know it does, as I've said, I'm already using Itpl extensively. And 
if there is consensus on the unreasonability of extending the 
language I can live with that and continue to use Itpl. See my other 
post a few minutes ago on this same thread for details.

> 
> At the moment, when you see a quoted string in Python source code
> you know it is a string literal, whose value is fixed at compile
> time.

Well, that still leaves a lot of room for interesting behavior. The 
value of the string is fixed, but what really happens with it when 
you run it through % isn't (and ultimately that's what matters, since 
the internal value of the format string is rarely of interest). Any 
class that implements __str__ can still give you interesting run-time 
surprises:

In [28]: class BlowUp:
   ....:   def __str__(self):
   ....:     1/0
   ....:
In [29]: x=BlowUp()
In [30]: s='x is %s'
In [31]: s
Out[31]= 'x is %s'
In [32]: s % (x,)
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call 
last)
 
?
 
? in __str__(self=<__main__.BlowUp instance>)
 
ZeroDivisionError: integer division or modulo by zero

See? Python is a dynamic language through and through, whether we 
like it or not.

By the way, that last thing wasn't meant to defend my interpolation 
idea, just to show that Python's dynamic behavior is very much at the 
core of the language, for better or worse. I happen to like it, 
actually, but I know it can open the door to very crazy  things.

Cheers,

f



More information about the Python-list mailing list