Skip to main content

Understanding Object-Oriented Programming (OOP) in Python

Introduction

Object-Oriented Programming (OOP) is a programming paradigm that provides a means of structuring programs by bundling related properties and behaviors into individual objects. In this tutorial, we will be discussing the core concepts of OOP in Python. This includes classes, objects, inheritance, encapsulation, and polymorphism.

Classes and Objects

At the core of Python's OOP system are classes and objects. A class is a blueprint for creating objects (a particular data structure), providing initial values for state (member variables), and implementations of behavior (member functions or methods).

# Define a class
class MyClass:
x = 5

An object is an instance of a class. You can create as many objects of a class as you need.

# Create an object
p1 = MyClass()
print(p1.x) # Prints: 5

The init Method

The init method in Python is a special method that gets called when an instance of the class is created. This method is defined with the keyword def followed by init(self):.

class Person:
def __init__(self, name, age):
self.name = name
self.age = age

p1 = Person("John", 36)

print(p1.name) # Prints: John
print(p1.age) # Prints: 36

Inheritance

Inheritance is a way of creating a new class using details of an existing class without modifying it. The newly formed class is a derived class (or child class). Similarly, the existing class is a base class (or parent class).

# Parent Class
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname

def printname(self):
print(self.firstname, self.lastname)

# Child Class
class Student(Person):
pass

x = Student("Mike", "Olsen")
x.printname() # Prints: Mike Olsen

In this example, we have two classes: Person and Student. The Student class is the child class of the Person class and it inherits all the properties and methods from the Person class.

Encapsulation

Encapsulation in Python is the process of wrapping up variables and methods into a single entity (class). In encapsulation, the variables of a class will be hidden from other classes, and can be accessed only through the methods of their own class.

class Computer:

def __init__(self):
self.__maxprice = 900

def sell(self):
print("Selling Price: {}".format(self.__maxprice))

def setMaxPrice(self, price):
self.__maxprice = price

c = Computer()
c.sell()

# change the price
c.__maxprice = 1000
c.sell()

# using setter function
c.setMaxPrice(1000)
c.sell()

Polymorphism

Polymorphism is an ability (in OOP) to use a common interface for multiple forms (data types). Suppose, we need to color a shape, there are multiple shape options (rectangle, square, circle). However we could use the same method to color any shape. This concept is called Polymorphism.

class Parrot:

def fly(self):
print("Parrot can fly")

def swim(self):
print("Parrot can't swim")

class Penguin:

def fly(self):
print("Penguin can't fly")

def swim(self):
print("Penguin can swim")

# common interface
def flying_test(bird):
bird.fly()

#instantiate objects
blu = Parrot()
peggy = Penguin()

# passing the object
flying_test(blu)
flying_test(peggy)

In the above program, we defined two classes Parrot and Penguin. Each of them have a common fly() method. However, their functions are different. To allow polymorphism, we created a common interface i.e flying_test() function that can take any object. Then, we passed the objects blu and peggy in the flying_test() function, it ran effectively.

Conclusion

And that wraps up our tutorial on Object-Oriented Programming in Python. We've covered a lot of ground, but there's still much more to explore. As always, the best way to learn is by doing. So, try to implement these concepts in your next Python project. Happy coding!