New submission from Paul McMillan <p...@mcmillan.ws>:

In Python 2.7, I can use an empty format string to indicate automatically 
numbered positional arguments when formatting the builtin string object.

http://docs.python.org/release/2.7/library/string.html#format-string-syntax

If I subclass string.Formatter, this expected functionality doesn't work.
http://docs.python.org/release/2.7/library/string.html#string-formatting


Python 2.7.2+ (default, Oct  4 2011, 20:06:09) 
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from string import Formatter
>>> class MyFormatter(Formatter):
...     pass
... 
>>> fs_a = 'a{0}b{1}c'
>>> fs_b = 'a{}b{}c'
>>> fs_a.format(1, 2)
'a1b2c'
>>> fs_b.format(1, 2)
'a1b2c'
>>> fmt = MyFormatter()
>>> fmt.format(fs_a, 1, 2)
'a1b2c'
>>> fmt.format(fs_b, 1, 2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/string.py", line 545, in format
    return self.vformat(format_string, args, kwargs)
  File "/usr/lib/python2.7/string.py", line 549, in vformat
    result = self._vformat(format_string, args, kwargs, used_args, 2)
  File "/usr/lib/python2.7/string.py", line 571, in _vformat
    obj, arg_used = self.get_field(field_name, args, kwargs)
  File "/usr/lib/python2.7/string.py", line 632, in get_field
    obj = self.get_value(first, args, kwargs)
  File "/usr/lib/python2.7/string.py", line 591, in get_value
    return kwargs[key]
KeyError: ''

----------
messages: 155708
nosy: PaulMcMillan
priority: normal
severity: normal
status: open
title: Custom string formatter doesn't work like builtin str.format
versions: Python 2.7

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue14297>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to