I feel stoopid ... this code is to verbose

Max M maxm at mxm.dk
Mon Jun 17 03:37:00 EDT 2002


ruud de rooij wrote:
> Max M <maxm at mxm.dk> writes:

> import locale
> locale.setlocale(locale.LC_NUMERIC, 'da_DK')
> locale.format('%.2f', 42000000.00, 1)
> 
> batteries-included-ly y'rs,


Yes this was actually one of the first things I tried, but I got a bit 
confused by the documentation::

"setlocale() is not thread safe on most systems. Applications typically 
start with a call of::

     import locale
     locale.setlocale(locale.LC_ALL, '')

This sets the locale for all categories to the user's default setting 
(typically specified in the LANG environment variable). If the locale is 
not changed thereafter, using multithreading should not cause problems.
"

So I worried that it would wreck havock with my application as I am 
using IIS, and I worried that the locale would be out of sync on 
different pages.

My misunderstanding was seeded by me reading the docs wrong. I though 
that if I set the locale to 'danish' that the built in functions str(), 
atof() etc. would change behind the scene to using different locales.

But off course it is only if I do something like::

     from locale import str
     locale.setlocale(locale.LC_NUMERIC, 'da_DK')
     str('%.2f', 42000000.00, 1)

that locale is used on str().

If I just do the below, I get no problems::

     import locale
     locale.setlocale(locale.LC_NUMERIC, 'da_DK')
     locale.str('%.2f', 42000000.00, 1)

So I worried for no reason, and will use the batteries instead of my 
home cooked function.

regards Max M




More information about the Python-list mailing list