Monday, May 20, 2024

Python OOP: a quick look

Ok let's make a simple class in Python... a constructor with a couple fields. A car with a couple attributes.
class Car:
    def __init__(self, name):
        self.name=name
    make=""
    model=""
Do I need to define some private fields first and do some get/set stuff, and then get tired of it and use lombok? No you don't. Do I even need pre-define my variables in constructor? No I don't because you define variables on the fly in Python. Ok I put it in a file Car.py. How do I load it in the prompt?
import Car
. Ok it let me. but how about
 mycar = Car("mycar") 
boom get an error. I need to do
from Car import *
Ok did that now I can do mycar("mycar"). Ok let's set some make and models.
mycar.make="Toyota"
mycar.model="Corolla"
So far so good. What about setting an undefined field like this?
 mycar.year=2020
Oh no errors, it let you do it! I guess I have to get used to the define variables and even fields on the fly. So you better make sure you type correctly. Waita minute, no public, private, protected? Did any OO aficiondos jump forward to blast Python? Is there things like reflective api to figure out what fields you have? There is this
dir(mycar)
to tells me what fields I have. "dir" is so DOS! There is a neato
vars(mycar)
to tell me what fields and values I got so far.

Ok, inheritance, no interface and "implements" vs "extends" differences as in Java. How about "abstract" as in C++? (There is. It is called the ABC) Oh you can even do multiple inheritance! (not worrying about the diamond problem here). What about static (instance) stuff? Yes there is. The variables on the constructor are instance attributes and those outside are class attributes. This is like a lot simpler (but is it better?) than other languages. Typos can be hairpulling as you can define variables on the fly.

Let me add a little method to make it tell me what we have

def showinfo() :
        print ("Car "+ name+" is a "+make+" "+model)
Boom it does not work!
>>> mycar.showinfo()
Traceback (most recent call last):
  File "<stdin>", line 1, in 
TypeError: showinfo() takes 0 positional arguments but 1 was given
What do you mean 1 was given, I didn't give you any arguments! (now that's an argument with the intepreter). and it turns out it needs a "self" argument. It does not know about itself! This worked:
def showinfo(self):
    print ("Car "+self.name+" is a "+self.make+" "+self.model)
Now to call it it is still mycar.showinfo(), no argument passed. Also, "self" is not a reserved word like "this" in C++/java, (or "self" or "me" like other languages). Anything goes but required. This also worked:
def showinfo(foofoo):
    print ("Car "+foofoo.name+" is a "+foofoo.make+" "+foofoo.model)
You just have to provide a dummy variable here. Jeez now that's wacky. I already tolerated the required tabs instead of blocks.

Plenty of tutorials about OOP out there such as this https://realpython.com/python3-object-oriented-programming/.

No comments: