[issue8985] String format() has problems parsing numeric indexes

Eric Smith report at bugs.python.org
Sat Jun 12 23:23:20 CEST 2010


Eric Smith <eric at trueblade.com> added the comment:

get_integer uses the narrowest possible definition for integer indexes, in order to pass all other strings to mappings.

>>> '{0[ 0 ]} {0[-1]}'.format({' 0 ': 'foo', '-1': 'bar'})
'foo bar'

Remember, it has to guess what type of lookup to do based on whether the value inside [] looks like an integer or not.

>From the PEP:
    Because keys are not quote-delimited, it is not possible to
    specify arbitrary dictionary keys (e.g., the strings "10" or
    ":-]") from within a format string.

I don't believe this restriction causes any practical problem.

I'm not sure the error could be improved. The code that's being called is essentially:

>>> [0, 1, 2]['-1']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: list indices must be integers, not str

----------
resolution:  -> rejected
status: open -> closed

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue8985>
_______________________________________


More information about the Python-bugs-list mailing list