Skip to main content

Python quick bite 02 - OOP

OOP - Object Oriented Programming 

From RealPython.com: "Object-oriented programming is a programming paradigm that provides a means of structuring programs so that properties and behaviors are bundled into individual objects."

To make use of this, we need to import the module and we can find many such packages available for use in the coding community. And how important it is to know how to read their documentation and make use of it. Thus allowing to code more complex systems. 

Basically, before we can create buildings aka objects, we need a blue-print and that is the class. The class will specify two aspects of the object, namely what is have = attributes and what it can do = methods. 

To create a Class
#we need to use Pascal Case for class naming to differentiate from variables naming which is in all lowercase.
eg 1. class WaterBottle:
#if you have an empty class, python can't run so we need to add "pass" to jump over this empty gap.
            pass

To create an object from a Class
eg 2.bottle1 = WaterBottle()
I create a waterbottle object called bottle1.

To add attributes to the create objects
mehod 1:
bottle1.capacity = 500
bottle1.volume = 50
 #this method can become tedious when you are creating many objects and you have to specify the many attributes. Eventually, if there is a spelling error in one of the attributes naming, it becomes a bug. 
method 2:
Using a constructor, the __init__() function (special function with 2 under scroll at each side of init), to specify the variables when an object is being constructed. 
In programming, this is called initializing. This function will be called every time an object is created from the class. 

To access the attributes of the bottle
print(bottle1.volume)
This will print the 500 which is the attribute of bottle1. 

To update my blueprint at eg1 with the init() function.
class WaterBottle:
    def __init__(self, b_capacity, b_volume):
        self.capacity = b_capacity
        self.volume = b_volume

And now to create a new object we need to specify capacity and volume at the start, else it cannot run. 
bottle2 = WaterBottle(800, 20)

I had created a bottle2 with a capacity of 800ml while only containing 20ml.
And you can check this by printing the attributes.
print(bottle2.capacity) 

We can also add methods into our class. Say, I like a refill function to my bottle to fill up the water to its capacity.
To create a method, we will add this into the class.
def refill(self):
    self.volume = self.capacity

To access the method of the bottle
bottle2.refill()
This will top up the volume to the capacity of the waterbottle.
And now if you print the bottle.volume again, it will be the capacity of the bottle. 
So when I print(bottle2.volume), it returned 800 now.

The full updated code is as below. 
class WaterBottle:
    def __init__(self, b_capacity, b_volume):
        self.capacity = b_capacity
        self.volume = b_volume

    def refill(self):
        self.volume = self.capacity

bottle2 = WaterBottle(800, 20)
print(bottle2.volume)
bottle2.refill()
print(bottle2.volume)

Output:
20
800

Inheritance from a Superclass allowing a class to inherit some basic traits from another overarching class. 
class MetalBottle(WaterBottle):
    def __init__(self):
        super().__init__() # recommended but strictly required

This allows the MetalBottle class to inherit all the attributes and methods in WaterBottle. To add on to the original method by WaterBottle class, we can do this.

def refill(self):
    super().refill()
    print(f"I am filled up to {self.volume}ml")
    

Comments