Python Forum
use of subclass - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: use of subclass (/thread-29725.html)



use of subclass - ebolisa - Sep-17-2020

Hi,

I cannot get the EUR subclass format correctly. What's the trick?

It should return 10.000,00 €.

TIA

from money import Money, exceptions

class USD(Money):
    def __init__(self, amount='0'):
        super().__init__(amount=amount, currency='USD')

balance = 10000

b = USD(str(balance))
b = b.format('en_US')
print(b)

print('\n************\n')

class EUR(Money):
    def __init__(self, amount='0'):
        super().__init__(amount=amount.format('it_IT'), currency='EUR')

bal = EUR(str(balance))
print(bal)



RE: use of subclass - jefsummers - Sep-17-2020

I get "No module named money". Can't test it further.


RE: use of subclass - ebolisa - Sep-17-2020

I didn't either so, I installed it:

https://pypi.org/project/money-lib/


RE: use of subclass - buran - Sep-17-2020

(Sep-17-2020, 12:37 PM)ebolisa Wrote: I didn't either so, I installed it:
creating sub-class for each currency is not the way to go. Creating class USD does not add benefit. You already have the currency information as property of Money class. Also what will be difference between USD and EUR class for example?


RE: use of subclass - jefsummers - Sep-17-2020

USD does not work either. You are subclassing from Money, which results in a conflict between the __new__ and __init__ routines. __new__ wants a currency but you are not giving one. If you supply a currency with your call in line 9 you now have too many arguments for your __init__ that you are trying to override.

At least from this code, it's far simpler just to use the Money functions and not subclass.