[Python-ideas] Draft PEP on string interpolation
Ron Adam
ron3200 at gmail.com
Thu Aug 27 20:21:52 CEST 2015
On 08/27/2015 04:17 AM, Eric V. Smith wrote:
>>> 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.
>> >
>> >Sure it does, you can access the format spec and apply it manually to
>> >each item or not. Is there another choice?
> You're not reading what I'm writing. Using your proposal of immediately
> converting to strings, how would you write the version of "to_list"
> whose output I show above?
>
>> >Depending on how you want to make the values and specs visible.
>> >
>> > def to_repr(istr):
>> > return ''.join(repr(item.format(spec)) for item, spec
>> > in istr.items())
Well, what you have above is applying repr to the values but not the
literal parts. It's doable, but that should really be part of the
format spec rather than applying a function from outside, or it should
be part of the expression in the i-strings.
i'{repr(n)}:{repr(s}}'
i'{n!r}:{s!r}'
Yes, this topic is getting too drawn out. Possibly I'm not seeing a
finer point in your examples. I think it will sort it self out as the
implementation progress's, so I'm not too worried about it.
I'll look at the code in your repository when I have time and try to
keep things to concrete examples that you can use in your tests if I
have time.
Cheers,
Ron
More information about the Python-ideas
mailing list