[Python-ideas] String interpolation for all literal strings

Nick Coghlan ncoghlan at gmail.com
Fri Aug 7 16:35:15 CEST 2015


On 7 August 2015 at 23:55, Eric V. Smith <eric at trueblade.com> wrote:
> If we do implement __interpolate__ as something like we're describing
> here, it again brings up the question of concatenating adjacent strings
> and f-strings. When I'm just calling expr.__format__ and joining the
> results, you can't tell if I'm turning:
>
> f'{a}' ':' f'{b}'
>
> into multiple calls to join or not. But if we used __interpolate__, it
> would make a difference if I called:
>
> __interpolate__('{0}:{1}', (a, b))
> or
> ''.join([__interpolate__('{0}', (a,)), ':', __interpolate('{0}', (b,))])

This is part of why I'd still like interpolated strings to be a
clearly distinct thing from normal string literals - whichever
behaviour we chose would be confusing to at least some users some of
the time. Implicit concatenation is fine for things that are actually
constants, but the idea of implicitly concatenating essentially
arbitrary subexpressions (as f-strings are) remains strange to me,
even when we know the return type will be a string object.

As such, I think the behaviour of bytes vs str literals sets a useful
precedent here, even though that particular example is forced by the
type conflict:

>>> b"asd" "asd"
 File "<stdin>", line 1
SyntaxError: cannot mix bytes and nonbytes literals

Regards,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list