[issue12014] str.format parses replacement field incorrectly
Eric V. Smith
report at bugs.python.org
Sat Jun 4 00:52:05 CEST 2011
Eric V. Smith <eric at trueblade.com> added the comment:
"""
>>> d = {"{0}": "spam"}
>>> # a matched pair of braces. What's inside is considered markup.
...
>>> "{0}".format(d)
"{'{0}': 'spam'}"
>>> # a matched pair of braces. Inside is a matched pair of braces, and what's inside of that is not considered markup.
"""
I'm not sure what' you're getting at. "{0}" (which is indeed "markup") is replaced by str(d), which is "{'{0}': 'spam'}".
"""
...
>>> "{0[{0}]}".format(d)
'spam'
>>>
"""
Again, I'm not sure what you're getting at. The inner "{0}" is not interpreted (per the PEP). So the entire string is replaced by d['{0}'], or 'spam'.
Let me try to explain it again. str.format() parses the string, looking for matched sets of braces. In your last example above, the very first character '{' is matched to the very last character '}'. They match, in sense that all of the nested ones inside match. Once the markup is separated from the character data, the interpretation of what's inside the markup is then done. In this example, there is no character data.
I apologize if I'm explaining this poorly.
----------
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue12014>
_______________________________________
More information about the Python-bugs-list
mailing list