[Python-ideas] Customizing format()

Nick Coghlan ncoghlan at gmail.com
Wed Mar 18 13:03:07 CET 2009


Eric Smith wrote:
> Antoine Pitrou wrote:
>> Raymond Hettinger <python at ...> writes:
>>> Am curious whether you guys like this proposal?
>>
>> I find it good for the builtin format() function, but how does it work
>> for
>> str.format()?
> 
> I agree: I like it, but it's not enough. I use str.format() way more
> often than I hope to ever use builtin format(). If we make any change,
> I'd rather see it focused on the format mini-language.

Perhaps we could add a new ! type to the formatting language that allows
the developer to mark a particular argument as the conventions
dictionary? Then you could do something like:

# DE and US dicts as per Raymond's format() example
fmt = "The value is {:,.5f}{!conv}"
fmt.format(num, DE)
fmt.format(num, US)
fmt.format(num, dict(thousands_sep='''))

As with !a and !s, you could use any normal field specifier to select
the conventions dictionary. Obviously, the formatting arguments would be
ignored for that particular field.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------



More information about the Python-ideas mailing list