monetary applications (et al)

terry tg5027 at citlink.net
Wed Sep 18 23:35:32 CEST 2002


>>>>> Chris wrote:
Well, sure. This is obviously a stupid way to go about...That's what 
object oriented programming is all about. What you need to do is create 
a currency class that behaves like it should, and then use it 
everywhere you want currency.  <snip>
>>>>>

I do admit to being OO inhibited, so maybe you could take it from this 
short piece code and execution results, and, provide a brief 
description of what you would put into this money class that would 
eliminate this error message, while maintaining the code simplicity.

-------------------------------------------------
class money:
	def __init__(self,name):
		self.name = name

unitprice = money(100)
qty = 2
amount = qty * unitprice

terry at terry:~> python money.py
Traceback (most recent call last):
  File "money.py", line 7, in ?
    amount = qty * unitprice
TypeError: unsupported operand type(s) for *: 'int' and 'instance'
-------------------------------------------------

Support for mixed number types in expressions involving this money 
class must come from somewhere - where?  And, it must be capable of 
determing and setting amount to be of class money to be consistent with 
the rest of Python's complexity hierarchy for the handling of numbers.

The rest of the problem is that the above is a trivial calculation.  
What happens to your recommendation when faced with an additional 
number type?

taxrate = 0.06
amount = qty * unitprice + (qty*unitprice)*taxrate

As I see it, if Python had an intrinsic number type of 'money', the 
logic for determing the results of the above calculation would be 
easily determined and consistent with expectations, as in VB.

Thanks for your time,

terry




More information about the Python-list mailing list