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

Eric V. Smith eric at trueblade.com
Tue Aug 11 02:04:38 CEST 2015


On 8/10/2015 8:00 PM, MRAB wrote:
> 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.

I think there are bug tracker issues for both of these. I think the
argument against changing them is that people might be depending on this
behavior. I'll grant you it seems unlikely, but you never know.

Eric.


More information about the Python-Dev mailing list