[Python-ideas] Briefer string format
Eric V. Smith
eric at trueblade.com
Sun Aug 2 18:00:59 CEST 2015
Eric.
On 8/2/2015 11:57 AM, MRAB wrote:
> On 2015-08-02 16:37, Eric V. Smith wrote:
>> On 8/1/2015 1:43 PM, Eric V. Smith wrote:
>>> On 7/25/2015 3:55 PM, Eric V. Smith wrote:
>>>> In trying to understand the issues for a PEP, I'm working on a sample
>>>> implementation. There, I've just disallowed concatentation entirely.
>>>> Compared to all of the other issues, it's really insignificant. I'll
>>>> put
>>>> it back at some point.
>>>
>>> I'm basically done with my implementation of f-strings.
>>
>> Here's another issue. I can't imagine this will happen often, but it
>> should be addressed. It has to do with literal expressions that begin
>> with a left brace.
>>
>> For example, this expression:
>>>>> {x: y for x, y in [(1, 2), (3, 4)]}
>> {1: 2, 3: 4}
>>
>> If you want to put it in an f-string, you'd naively write:
>>
>>>>> f'expr={{x: y for x, y in [(1, 2), (3, 4)]}}'
>> 'expr={x: y for x, y in [(1, 2), (3, 4)]}'
>>
>> But as you see, this won't work because the doubled '{' and '}' chars
>> are just interpreted as escaped braces, and the result is an
>> uninterpreted string literal, with the doubled braces replaced by
>> undoubled ones.
>>
>> There's currently no way around this. You could try putting a space
>> between the left braces, but that fails with IndentationError:
>>
>>>>> f'expr={ {x: y for x, y in [(1, 2), (3, 4)]}}'
>> File "<fstring>", line 1
>> {x: y for x, y in [(1, 2), (3, 4)]}
>> ^
>> IndentationError: unexpected indent
>>
> Why is there an IndentationError? It's an expression, not a statement,
> so leading spaces should be ignored.
Good question. I'm parsing it with PyParser_ASTFromString. Maybe I'm
missing a compiler flag there which will ignore leading spaces.
But in any event, the result is the same: You'll need to add a space
here in order to disambiguate it from doubled braces. That's really the
crux of the issue.
Eric.
More information about the Python-ideas
mailing list