[Python-ideas] Rough draft: Proposed format specifier for a thousands separator (discussion moved from python-dev)

Raymond Hettinger python at rcn.com
Fri Mar 13 00:57:19 CET 2009


>> I've always thought that we should have a utility function which formats 
>> a number based on the same settings that are in the locale, but not 
>> actually use the locale. Something like:
>> 
>> format_number(123456787654321.123, decimal_point=',', thousands_sep=' ',
>>               grouping=[4, 3, 2])
>>  >>> '12 34 56 78 765 4321,123'
> 
> To be maximally useful (for example, so it could be used in Decimal to 
> implement locale formatting), maybe it should work on strings:
> 
> >>> format_number(whole_part='123456787654321',
>               fractional_part='123',
>               decimal_point=',',
>               thousands_sep=' ',
>               grouping=[4, 3, 2])
> >>> '12 34 56 78 765 4321,123'

Whoa guys!  I think you're treading very far away from and rejecting the 
whole idea of PEP 3101 which was to be the one ring to bind them all with
format(obj, fmt) having just two arguments and doing nothing but
passing them on to obj.__fmt__() which would be responsible for
parsing a format string.

Also,even  if you wanted a flexible clear separate tool just for number
formatting, I don't think keyword arguments are the way to go.
That is a somewhat heavy approach with limited flexibility.
The research in PEP 378 shows that for languages needing
fine control and extreme versatility in formatting, some kind
of picture string is the way to go.  MS Excel is a champ
at number/date formatting strings:   #,##0 and whatnot.
The allow negatives to have placeholders, trailing minus signs,
parentheses, etc.  Columns can be aligned neating, any type
of padding can be used, any type of separator may be specified.
The COBOL picture statements also offer flexibility and clarity.
Mini-languages of some sort beat the heck out of functions
with a zillion optional arguments.


Raymond

"Working with creative thinkers can be like herding cats."





More information about the Python-ideas mailing list