Python – Thoughts about recent lessons.

Spent more time playing with recursion, as much as I like this was still confused. Recursion is very confusing to break down into steps, the fibonacci recursive function was written three ways. The third time was for a test, this time it used range() to show the sequence starting from 0 to range(n), which if its range(6) would be 0,1,2,3,4,5. So its used as the ordering mechanism 0,1,1,2,3,5 the start of the fib sequence.

Anyway that third time really helped me break down the process with an even smaller function. Now I moved on to OOP lessons, I have done lots of these in a few other languages and figured it would be decently simple. WRONG, Python OOP is really crazy so far, like almost a language hacked. Classes that inherit each other is pretty tricky looking to the eyes, overloading magic methods is mind fucking me. Woah new stuff or things done differently than what I was expecting.

Maybe I was just not having a good learning day and stuff seemed crazy, to test this I play some games of Chess and I did alright. The code below was the easiest little code to fix in my lessons, btw SoloLearn has an excellent Python course. It is covering in detail more than the previous excellent one ‘Python for everybody’.

A book that is more of a quick reference with examples is ‘Python Distilled’ highly recommended.

# A simple operator overload for + add, this was new to me.
class BankAccount:
    def __init__(self, balance):
        self.balance = balance

    def __add__(self,other):
        return BankAccount(self.balance + other.balance)
# Basically without this trying to use + to add two string values a + b
# would error, so we have overloaded using __add__ and we can
# now add two strings with number values as the result object below.

a = BankAccount(1024)
b = BankAccount(42)

# Woah we have hax0red the + operator in BankAccount class.
result = a + b

So guess what many other operators can be overloaded too.

Leave a Comment