Rough draft: Proposed format specifier for a thousands separator

MRAB google at mrabarnett.plus.com
Mon Mar 16 03:36:43 CET 2009


Rhodri James wrote:
> On Sun, 15 Mar 2009 19:00:43 -0000, MRAB <google at mrabarnett.plus.com> 
> wrote:
> 
>> Rhodri James wrote:
>> [snip]
>>> Frankly, I'd much rather fix the locale system and extend
>>> the format syntax to override the default locale.  Perhaps
>>> something like
>>>    financial = Locale(group_sep=",", grouping=[3])
>>>   print("my number is {0:10n:financial}".format(1234567))
>>>  It's hard to think of a way of extending "%" format strings
>>> to cope with this that won't look utterly horrid, though!
>>>
>> The problem with your example is that it magically looks for the locale
>> name "financial" in the current namespace.
> 
> True, to an extent.  The counter-argument of "Is it so much
> more magical than '{keyword}' looking up the object in the
> parameter list" suggests a less magical approach would be to
> make the locale a parameter itself:
> 
>   print("my number is {0:10n:{1}}".format(1234567, financial)
> 
The field name can be an integer or an identifier, so the locale could
be too, provided that you know where to look it up!

     financial = Locale(group_sep=",", grouping=[3])
     print("my number is {0:10n:{fin}}".format(1234567, fin=financial))

Then again, shouldn't that be:

     fin = Locale(group_sep=",", grouping=[3])
     print("my number is {0:{fin}}".format(1234567, fin=financial))

>> Perhaps the name should be
>> registered somewhere like this:
>>
>>      locale.predefined["financial"] = Locale(group_sep=",", grouping=[3])
>>      print("my number is {0:10n:financial}".format(1234567))
> 
> I'm not sure that I don't think that *more* magical than my
> first stab!  Regardless of the exact syntax, do you think
> that being able to specify an overriding locale object (and
> let's wave our hands over what one of those is too) is the
> right approach?
> 




More information about the Python-list mailing list