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

MRAB python at mrabarnett.plus.com
Tue Aug 11 02:00:15 CEST 2015


On 2015-08-11 00:26, Victor Stinner wrote:
> Le mardi 11 août 2015, Eric V. Smith <eric at trueblade.com
> <mailto:eric at trueblade.com>> a écrit :
>
>     It sounds like you want to disallow leading spaces just to
>     disallow this one type of expression.
>
>
>   I would like to reduce the number of subtle differences between
> f-string and str.format().
>
I'm a little bit surprised at seeing this:

 >>> '{0}'.format('foo')
'foo'
 >>> '{ 0}'.format('foo')
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
KeyError: ' 0'
 >>> '{a}'.format(a='foo')
'foo'
 >>> '{ a}'.format(a='foo')
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
KeyError: ' a'

In some other cases, leading and trailing spaces are ignored:

 >>> int(' 0 ')
0

Outside string literals, they're also ignored.

But, then:

 >>> '{-1}'.format('foo')
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
KeyError: '-1'

It's a string key, even though it looks like an int position.



More information about the Python-Dev mailing list