[Python-ideas] Briefer string format

Eric V. Smith eric at trueblade.com
Thu Jul 23 00:29:37 CEST 2015


On 7/22/2015 4:21 PM, MRAB wrote:
> On 2015-07-22 19:52, Eric V. Smith wrote:
>> On 07/20/2015 03:22 PM, Guido van Rossum wrote:
>>> Not sure what you mean by "implicit merging" -- if you mean literal
>>> concatenation (e.g. 'foo' "bar" == 'foobar') then I think it should be
>>> allowed, just like we support mixing quotes and r''.
>>
>> Do we really want to support this? It complicates the implementation,
>> and I'm not sure of the value.
>>
>> f'{foo}' 'bar' f'{baz}'
>> becomes something like:
>> format(foo) + 'bar' + format(baz)
>>
>> You're not merging similar things, like you are with normal string
>> concatenation.
>>
>> And merging f-strings:
>> f'{foo}' f'{bar'}
>> similarly just becomes concatenating the results of some function calls.
>>
>> I guess it depends if you think of an f-string as a string, or an
>> expression (like the function calls it will become). I don't have a real
>> strong preference, but I'd like to get it ironed out logically before
>> doing a trial implementation.
>>
> As Guido said, we can merge raw string literals.

True, but f-strings aren't string literals. They're expressions
disguised as string literals.

> It would be a gotcha if:
> 
>     r'{foo}' 'bar'
> 
> worked but:
> 
>     f'{foo}' 'bar'
> 
> didn't. You'd then have to how they're different even though they look
> a lot alike.

While they look alike, they're not at all similar. Nothing is being
merged, since the f-string is being evaluated at runtime, not compile time.

I'm not sure if it would be best to hide this runtime string
concatenation behind something that looks like it has less of a cost.

At runtime, it's likely going to look something like:
''.join([f'foo', 'bar'])
although using _PyUnicodeWriter, I guess.

Eric.


More information about the Python-ideas mailing list