[Python-Dev] PEP 498 f-string: please remove the special case for spaces

Eric V. Smith eric at trueblade.com
Tue Aug 11 01:04:17 CEST 2015


On 8/10/2015 6:54 PM, Victor Stinner wrote:
> 
>     PEP 498:
> 
> """
> 
> 
>     Leading whitespace in expressions is skipped
>     <https://www.python.org/dev/peps/pep-0498/#id28>
> 
> Because expressions may begin with a left brace ('{'), there is a
> problem when parsing such expressions. For example:
> 
>>>> f'{{k:v for k, v in [(1, 2), (3, 4)]}}' '{k:v for k, v in [(1, 2), (3, 4)]}'
> 
> """
> 
> For me, this example is crazy. You should not add a special case (ignore
> spaces) just to support a corner case.
> 
> This example can easily be rewritten using a temporary variable and it
> makes the code simpler.
> 
> items={k:v for k, v in [(1, 2), (3, 4)]; f'{items}'
> 
> Seriously, a dict-comprehension inside a f-string should be considered
> as an abuse of the feature. Don't you think so?

Yes, it's absolutely an abuse and should never be written. But if the
only "cost" to allowing it is skipping leading spaces, I don't see the
harm. It sounds like you want to disallow leading spaces just to
disallow this one type of expression.

My other use case for spaces, which I've not added to the PEP yet, is
for alignment, like:

f' x={ x:15}'
f'xx={xx:15}'

Eric.


More information about the Python-Dev mailing list