My Object Oriented program is too tedious

Hello all. I am highly interested in Machine learning(ML) & Deep learning(DL), that’s why I started learning Python on Aug. 20 2023.
However, my mindset told me I am losing passion to learn Python because I can’t withstand the ugliness of my programs. I majored in Applied Physics in college and my MS thesis was written in C in 2000. Back then, my thesis advisor always trained me, Proper Algorithms 1st, programs 2nd.
Yesterday, I spent 15 hours solving the following exercise in my
e-book by trials & errors experiments which were typical in my MS training 'cause I was an applied physics student. But I had no happiness after solving the exercise 'cause I don’t like it.
My hero here is Chris Angelico Rosuav whose Python programs are always readable, simple, and hence BEUTIFUL. My mindset keeps telling myself Proper Algorithms, Proper Algorithms, and still, Proper Algorithms. Otherwise, I am afraid I can’t learn ML & DL. I know there are lots of Python experts here in addition to Chris Angelico Rosuav. Can any experts pore through my OO program below and advise me what a proper Python algorithm can solve the following OOP?Moreover, is there any online documentation which tells the Python programmers what a good python algorithm is? Million thanks.

The exercise: Task: The exchange rate between SGD and Euro is 1 to 1.8. Do some arithmetic operations with SGD and Euro.
( 2 ) Subtract 20 SGD from 40 EURO and give your answer in SGD.
( 3 ) Subtract 20 Euro from 100 SGD and give your answer in Euro.

The body of my program:

``````exchange = {"SGD":{"Euro":1.8}}
class Money:
def __init__(self, amount,currency):
self.amt  = amount
self.currency = currency
if isinstance(money,self.__class__):
if self.currency == money.currency:
return Money(self.amount+money.amt,self.currency)
else:
converted_rate =exchange[self.currency [money.currency] *
money.amt
return Money(self.amt+converted_rate,self.
currency) = exchange[self.currency [money.currency] * money.amt
return Money(self.amt+converted_rate,self.currency)
def __sub__(self,money):
money.amt *  =  -1

def convert(self, currency):
converted_rate = exchange[self.currency] [currency] * self.amt
return Money(converted_rate,currency)
def __repr__(self):
return "The amount is {} in {}" .format(round(self.amt,2),self.currency)
``````

( 1 ) Money(20,‘SGD’) + Money(40,‘Euro’)

" The amount is 92.0 in SGD "

( 2 ) Money(20,‘SGD’) - Money(10, ‘Euro’)
" The amount is 2.0 in SGD "

( 3 ) m = Money(100,‘SGD’) - Money(20, ‘Euro’)
m.convert(‘Euro’)
" The amount is 115.2 in Euro "

Please format your code by placing them inside ``` (triple backtick) block, and with proper indentation. It’s very hard to read it as is.

2 Likes

1 Like

Is this you? If so please don’t spam questions!
Now to the question: Maybe have the money exchange be its own class/function. Also, why do you think this code is bad?

The only serious problem I see with your code is this part

``````def __sub__(self,money):
money.amt *  =  -1

``````

`money.amt *= -1` will modify `money` in-place, which will yield incorrect result like this

``````a = Money(6, "Euro")
b = Money(5, "SGD")
print(b) # The amount is 5.0 in SGD
c = a - b # according to your code, b.amt *= -1, meaning its value is now -5
print(b)  # The amount is -5.0 in SGD
``````

You should change it to:

``````def __sub__(self,money):
# do not modify money, but create a new object
negative_money = Money(-money.amt, money.currency)
``````
1 Like

Maybe beauty is in the eye of the beholder, but I think overall the code looks fine.

Perhaps the only modification I would suggest for readability is the `__repr__` function, you could write it like this

``````def __repr__(self):
return f"The amount is {self.amt:.2f} in {self.currency}"
``````

This syntax is called f-string and it’s relatively new, so beginners following old tutorials may not know about it.

1 Like

If you start learning DL tomorrow then you should learn what proper algorithms of DL are, don’t you think? As to the “simple and hence easy” part, this is quite a complex topic and very personal since every person has their own definition of readable and good code, but a lot of people swear on “Clean Code”, so I’ll just recommend reading that.

1 Like

You can reduce the “trial and error” process by using a static type checker such as `mypy` or `pyright`. They will flag fundamental mistakes such as indentation error, undefined variable, etc. (and more if you also type hint your code, but that’s more advanced) in your IDE, so that you can quickly fix them instead of having to run the code to find out.

1 Like

Just google some books/blogs/tutorials together, especially about the theory and math behind it. For example I quickly found this list of resources: Basics of machine learning with TensorFlow. I also recommend you use machine learning frameworks (like TensorFlow) because writing one yourself will need thousands of lines. Machine learning is not an easy topic and you will likely need to study for a few more months before coming around to using it.

1 Like

Author attempting to bypass moderation actions.