This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author rhettinger
Recipients rhettinger
Date 2009-05-21.23:23:13
SpamBayes Score 6.037105e-10
Marked as misclassified No
Message-id <1242948196.62.0.545811632191.issue6081@psf.upfronthosting.co.za>
In-reply-to
Content
The old % formatting allowed arbitrary mappings:

  >>> class Default(dict):
  ...     def __missing__(self, key):
  ...         return key
  ...
  >>> s = '%(name)s was born in %(country)s'
  >>> s % Default(name='Guido')
  'Guido was born in country'

But the new str.format() demands mappings be first converted to a
regular dict using **kwargs, so something like the above is not possible.

  >>> s = '{name} was born in {country}'
  >>> s.format(**Default(name='Guido'))
  Traceback (most recent call last):
    File "<pyshell#27>", line 1, in <module>
      s.format(**Default(name='Guido'))
  KeyError: 'country'

There is a work-around using string.vformat() but it is obscure and awkward:

  >>> import string
  >>> string.Formatter().vformat(s, (), Default(name='Guido'))
  'Guido was born in country'

Instead, it would be better to offer an alternate method:

  >>> s.format_from_mapping(Default(name='Guido'))
  'Guido was born in country'
History
Date User Action Args
2009-05-21 23:23:16rhettingersetrecipients: + rhettinger
2009-05-21 23:23:16rhettingersetmessageid: <1242948196.62.0.545811632191.issue6081@psf.upfronthosting.co.za>
2009-05-21 23:23:15rhettingerlinkissue6081 messages
2009-05-21 23:23:14rhettingercreate