[Python-Dev] Fwd: deep question re dict as formatting input

Nick Coghlan ncoghlan at gmail.com
Wed Feb 23 14:42:21 CET 2011


On Wed, Feb 23, 2011 at 9:32 AM, Senthil Kumaran <orsenthil at gmail.com> wrote:
> """
> 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 was curious as to whether or not nested substitution could be used
to avoid that limitation, but it would seem not:

>>> "{0[{1}]}".format(d, 21.2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: '{1}'

Turns out that was also a deliberate design choice:

"""
These 'internal' replacement fields can only occur in the format
specifier part of the replacement field.  Internal replacement fields
cannot themselves have format specifiers.  This implies also that
replacement fields cannot be nested to arbitrary levels.
"""

Ah, how (much more) confused would we be if we didn't have the PEPs
and mailing list archives to remind ourselves of what we were thinking
years ago...

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-Dev mailing list