[Python-ideas] Draft PEP on string interpolation

Eric V. Smith eric at trueblade.com
Thu Aug 27 04:41:43 CEST 2015


On 8/26/2015 9:08 PM, Ron Adam wrote:
> If you store a string instead of the value, then mutating the object
> won't effect the i-string.  Also you don't get held references to
> objects that may be more expensive than a string.
> 
> I think these points need to be in the PEP.

Well, it's Nick's PEP, so you'll have to convince him.

Here I'll talk about my ideas on i-strings, which I've been implementing
on that bitbucket repo I've posted. Although I believe they're
consistent with where Nick is taking PEP 501.

As I've said before, it's not possible for an i-string to convert all of
its expressions to text when the i-string is first constructed. The
entire point of delaying the interpolation until some point after the
object is constructed is that you don't know how the string conversion
is going to be done.

Take this i-string:
i'value: {value}'

How would you convert value to a string before you know how it's being
converted, or even if it's being converted to a string? What if you use
a conversion function that converts the i-string to a list, containing
the values of the expressions? Or maybe your converter is going to call
repr() on each expression. If you convert to a string first, you've
destroyed information that the converter needs.

n = 10
s = 'text'
x = i'{n}:{s}'

to_list(x)  -> [10, ':', 'text']
to_repr(x)  -> '10:"text"'

And this doesn't even take into account the format_specs or conversions,
which only have meaning to the conversion function.

Eric.


More information about the Python-ideas mailing list