[issue5237] Allow auto-numbered replacement fields in str.format() strings

Eric Smith report at bugs.python.org
Mon Mar 9 13:17:04 CET 2009


Eric Smith <eric at trueblade.com> added the comment:

I'm attaching a patch that delivers the basic functionality in
str.format. This patch is against trunk, although it will probably work
elsewhere.

DO NOT USE THIS PATCH IN ANY SERIOUS WORK

It doesn't implement all of the needed functionality, it probably has a
memory leak, and it most likely can cause a GP fault. It does, however,
handle the common cases and it will give you a taste of the feature.

In particular, it doesn't handle anything other than empty braces
('{:d}' will fail). I can add this, and I think it should be added, but
I just haven't had the time to finish it up. I wanted to get this posted
so others can play with it and we can reach a decision if we want to add
this functionality. Personally, I think it's a great improvement, and
the more I play with it the more I like it.

If we reach a consensus that the feature should be added, I can probably
get it cleaned up and finished before PyCon.

$ ./python.exe 
Python 2.7a0 (trunk:70244M, Mar  8 2009, 16:54:23) 
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> '{} {} {}'.format(1, 3.4, 'test')
'1 3.4 test'
>>> '{} {1}'.format(1, 2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: cannot switch from automatic field numbering to manual field
specification
>>>

----------
keywords: +patch
message_count: 14.0 -> 15.0
Added file: http://bugs.python.org/file13283/half-baked-not-for-real-use.patch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue5237>
_______________________________________


More information about the Python-bugs-list mailing list