Whatever happened to String Interpolation?

Fernando Pérez fperez528 at yahoo.com
Fri Nov 30 19:45:40 EST 2001


Tim Hammerquist wrote:

> Fernando Pérez <fperez528 at yahoo.com> graced us by uttering:
> [ snip ]
>>         "x is $x, f(x) is $f(x)"
>> 
>> This to me is readable, unambiguous and very useful.
> 
> Ambiguity:
> 
>     Is that "the result of function f on variable x" or "the value
>     of variable f, with the literal '(x)' after it"?

I don't see the ambiguity. Here's a transcript of what Itpl (Ping's 
'reference' implementation):

In [1]: from Itpl import itpl
In [2]: x=5
In [3]: itpl('x is $x, f(x) is $f(x)')

# this fails, f is undefined:
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call 
last)

? in itpl(text='x is $x, f(x) is $f(x)')
? in __str__(self=<Itpl 'x is $x, f(x) is $f(x)'>)
 
NameError: name 'f' is not defined

In [4]: f='NAME F'

# this fails, you want to call a string:
In [5]: itpl('x is $x, f(x) is $f(x)')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call 
last)
 
? in itpl(text='x is $x, f(x) is $f(x)')
? in __str__(self=<Itpl 'x is $x, f(x) is $f(x)'>)
 
TypeError: object of type 'string' is not callable

In [6]: def f(x): return x**2
   ...:

# now f is a function, all is well:
In [7]: itpl('x is $x, f(x) is $f(x)')
Out[7]= 'x is 5, f(x) is 25'

# if you want f to be something else, you protect the name just like 
# in a normal shell. No need to go as crazy as you said in @{[&f(x)]}:

In [8]: f='NAME F'
In [9]: itpl('x is $x, f(x) is ${f}(x)')
Out[9]= 'x is 5, f(x) is NAME F(x)'


> This slippery slope is one of many reasons many Pythonistas don't
> want creeping featurism to infect their language.

I honestly don't see it as a slippery slope. Ping's implementation 
seems to 'do the right thing' in all cases, produces no surprises and 
doesn't at all require the kind of grossness you quoted.

> (Perl has ways of disambiguating(?) the above expression, but it
> leads to ugly code Pythonistas love to make fun of. eg:
> 
>     "x is $x, f(x) is @{[&f(x)]}"
> 
> That's the perl equivalent. )

I'm not trying to be stubborn, I'd just like to see if this topic can 
be discussed a bit more. For regexps the \\\\\\ plague was deemed bad 
enough that the best solution was to introduce a special type of 
string, r''. Unicode strings also are special. I don't see 
interpolated strings as being a 'slippery slope', the concept of 
special strings is *already* in the language.

I certainly *don't* want  to see Python go the perl route (and I've 
written a lot of perl, so I know exactly what it's like). But that 
doesn't mean that there aren't things where python's current syntax 
can actually end up leading to unnecessary ugliness.

In fact, things like 'blah %(var)s' to me look a lot less readable 
(and line noise like, perl-like, whatever) than 'blah $var'. Or is it 
that in Python there's such a rabid fear of $ as to not even admit a 
case where something like it may be useful?

Regards,

F.



More information about the Python-list mailing list