[Python-ideas] Added a function to parse str.format() mini-language specifiers

Eric Smith eric at trueblade.com
Mon Mar 16 15:23:25 CET 2009


I'd like to add a function (or method) to parse str.format()'s standard 
mini-language format specifiers. It's hard to get right, and if PEP 378 
is accepted, it gets more complex.

The primary use case is for non-builtin numeric types that want to add 
__format__, and want it to support the same mini-language that the built 
in types support. For example see issue 2110, where Mark Dickinson 
implements his own version for Decimal, and suggests it be moved elsewhere.

This function exists in Objects/stringlib/formatter.h, and will just 
need to be exposed to Python code. I propose a function that takes a 
single str (or unicode) and returns a named tuple with the appropriate 
values filled in.

So, is such a function desirable, and if so, where would it go? I could 
expose it through the string module, which is where the sort-of-related 
Formatter class lives.

It could be a method on str and unicode, but I'm not sure that's most 
appropriate.

Eric.



More information about the Python-ideas mailing list