Message155708
| Author |
PaulMcMillan |
| Recipients |
PaulMcMillan |
| Date |
2012年03月14日.02:02:58 |
| SpamBayes Score |
2.9049874e-11 |
| Marked as misclassified |
No |
| Message-id |
<1331690580.32.0.967691211794.issue14297@psf.upfronthosting.co.za> |
| In-reply-to |
| Content |
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: '' |
|
History
|
|---|
| Date |
User |
Action |
Args |
| 2012年03月14日 02:03:00 | PaulMcMillan | set | recipients:
+ PaulMcMillan |
| 2012年03月14日 02:03:00 | PaulMcMillan | set | messageid: <1331690580.32.0.967691211794.issue14297@psf.upfronthosting.co.za> |
| 2012年03月14日 02:02:59 | PaulMcMillan | link | issue14297 messages |
| 2012年03月14日 02:02:58 | PaulMcMillan | create |
|