[Python-ideas] [Python-Dev] User conversions in custom string.Formatter

Eric Smith eric at trueblade.com
Wed Mar 16 02:32:35 CET 2011

On 3/15/2011 9:07 PM, Andrew Svetlov wrote:
> Ooh. Sorry. Of course I used
> s = Formatter().format("{0!q}, {1!t}", a, b)
> I don't want to put those specific conversions into standard - that
> doesn't make sense as you said.
> MyFormatter().format(...) was intended to extend standard formatting
> mechanic and it works pretty well for me.
> But I can use only single-character as converter names.
> PEP 3101 doesn't limit that explicitly, it only says:
>   - there are !s and !r (later added !a) converters
>   - a custom Formatter class can define additional conversion flags
> You cannot specify multi-char converter name because
> string:Formatter.parse (and maybe string:Formatter.get_field)
> cannot parse that form.

You can get whatever behavior you want by overriding the .parse() method 
in addition to convert_field.

More information about the Python-ideas mailing list