topic: Intro OOP(Object-oriented Programming) for Python

Tags oop

This is a reading presentation on Object-oriented Programming (OOP) for Python.

Note

This is an introduction, this is just to give you a clearer perspective on OOP.

Youtube videos

Reading

https://realpython.com/python3-object-oriented-programming/

Some Example Code

The code below demonstrates composition and a little bit of inheritance. There is a lot more to be said about OOP.


import random


class Wheel:
    def __init__(self):
        self.tread = 1000

    def still_ok(self):
        return self.tread > 0

    def use(self, speed):
        self.tread -= random.random() * 10 * speed


class Vehicle:
    # colour = "red"
    def __init__(self, color):
        self.color = color


class Helicopter(Vehicle):
    pass


class Car(Vehicle):
    has_fueltank = True

    def __init__(self, speed=1, wheels=4):
        print(f"entering constructor: {self} at address {id(self)}")
        # super(Car,self).__init__(color="red")
        self.position = 0
        self.speed = speed
        # self.wheels = [Wheel() for i in range(wheels)]
        self.wheels = []
        for _ in range(wheels):
            self.wheels.append(Wheel())
        print("exiting constructor")

    def drive_forward(self):
        print("driving forwards")
        self.position += self.speed
        for wheel in self.wheels:
            wheel.use(self.speed)

    def needs_service(self):
        if self.position >= 10000:
            return True
        for wheel in self.wheels:
            if not wheel.still_ok():
                return True
        return False


RAW CONTENT URL