prePEP: Money data type

Alex Martelli aleaxit at yahoo.com
Tue Oct 21 18:28:12 EDT 2003


Batista, Facundo wrote:
   ...
> #- A 'format' method could take optional arguments specifying which
> #- locale to use, whether to use money or basic conventions,
> #- international or basic ones, etc.  THAT would be of some use,
> #- of course.  A __str__ method cannot take such arguments,
> #- and IMHO should therefore default to simplicity -- just
> #- '12.34' (so I can use a '$%8s' % m to get the kind of format
> #- shown in the above columnar output).
> 
> OK. See your point and agree with it.
> 
> But .format should be very complicated (accepting different arguments,
> etc, etc) or should be very simple (use '$' and '.', and if you want it
> different, overload it)?

I don't know.  Using '$' is silly (half of Python's users are in countries
where the $ doesn't rule; around here, maybe you've been to Rimini
when you were studying here, it's mostly found in the Tempio
Malatestiano, as Sigismondo and Isabella's heraldic device [an S and
an I entwined]).  Doing thousands-separation is cool if the users
want it (many do) but then you might as well get the localeconv()
plus optional indications to format things internationally rather than
locally (USD instead of $, etc).

I think, but I don't _know_, that:

m.format()

should DEFAULT to using the current locale.localeconv(); a full
call would have:

m.format(localeconv=somelocaleconvlikedict, formatkind='international')

default localeconv being locale.localeconv(), default formatkind being
"plain" (no currency symbol), alternatives 'local' (e.g. '$') or 
'international' (e.g. 'USD').  I.e., 2 optional args, a localeconv-like 
dict (of which you'll only use and thus only check a few entries),
the other a string or maybe Money.FORMAT_INTERNATIONAL &c
if you feel grandiose, to pick one of the 3 formatting styles.

Better names are no doubt easily found for these args:-).

But if one feels grand one MIGHT take arbitrary other keyword args
and do something like:
def format(self, **kwds):
    localconv = kwds.pop('localeconv', locale.localeconv()).copy()
    formatkind = kwds.pop('formatkind', 'international')
    localconv.update(kwds)
    # proceed...

this MIGHT become a tad slow, but then, power has a price...:-).


Alex





More information about the Python-list mailing list