prePEP: Money data type

Ian Bicking ianb at colorstudy.com
Mon Oct 20 21:41:38 EDT 2003


On Monday, October 20, 2003, at 01:49 PM, Batista, Facundo wrote:
> #- As a side note, it's quite possible to spend inordinate
> #- amounts of attention on formatting.  I would suggest that
> #- a fundamental 'money' class should first of all nail down
> #- the *computation* issues (just that variety of rounding
> #- specs is gonna be SUCH a bear...!) and receiving and
>
> It's very temptating to str() just represent the number, and if you 
> want
> something else, overload it.
>
> The problem is that decimalSeparator and thousandSeparator are 
> important to
> parse the string in the constructor (if I need str() to give me a ',' 
> as
> decimal point, I also need to supply a ',' as decimal point to the
> constructor).

Such a "smarter" constructor should perhaps be a separate class method, 
or be explicitly controlled with extra arguments.  Subclasses could 
still add this functionality, but the generic Money class would remain 
generic.

For instance, Money.parse('$100.00'), Money.parse('$100.00', 
decimalSeparator='.'), Money.parse('100Y', currency='Y'), etc.

Also, while you can use potentially ambiguous heuristics for this parse 
constructor, the main constructor can be more strict -- it's hard to 
add strictness, but easier to take it away.  I would expect that the 
main constructor take only a string or a number-like-object (int, 
float, fixed point, etc), with other information passed through keyword 
arguments.  I would prefer the string be a Python style of number, 
i.e., unambiguously and strictly with '.' for decimals, and no 
thousands-separator.  But then that matches my locale, so I suppose I'm 
biased -- but I see the sole reason for using that string to be that 
Python float literals cannot precisely express a fixed-point number.  
If a fixed-point literal existed, then I would prefer that no string be 
allowed -- the parse constructer better highlights what is being done 
in that case.

(A fixed-point literal would be quite nice... a fixed-point builtin 
would be quite nice...)

--
Ian Bicking | ianb at colorstudy.com | http://blog.ianbicking.org






More information about the Python-list mailing list