[Python-ideas] Briefer string format
Chris Angelico
rosuav at gmail.com
Mon Aug 3 01:30:12 CEST 2015
On Mon, Aug 3, 2015 at 1:37 AM, Eric V. Smith <eric at trueblade.com> wrote:
> 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
>
> In the PEP I'm going to specify that leading spaces are skipped in an
> expression. So that last example will now work:
>
>>>> f'expr={ {x: y for x, y in [(1, 2), (3, 4)]}}'
> 'expr={1: 2, 3: 4}'
>
> Note that the right braces in that last example aren't interpreted as a
> doubled '}'. That's because the first one is part of the expression, and
> the second one ends the expression. The only time doubling braces
> matters is inside the string literal portion of an f-string.
Sounds good. And even though your }} is perfectly valid, I'd recommend
people use spaces at both ends:
>>> f'expr={ {x: y for x, y in [(1, 2), (3, 4)]} }'
'expr={1: 2, 3: 4}'
which presumably would be valid too. It's a narrow enough case
(expressions beginning or ending with a brace) that the extra spaces
won't be a big deal IMO.
ChrisA
More information about the Python-ideas
mailing list