A Beginner’s Guide to Object-Oriented Programming in Python

Unlocking the power of OOP in Python: Understanding Classes, Objects, Inheritance and Polymorphism

Manish Salunke
4 min readJan 16, 2023
Oriented Programming in Python
Photo by Mohammad Rahmani on Unsplash

Object-oriented programming (OOP) is a programming paradigm that is based on the concept of “objects”, which can contain data and code that manipulates that data. Python is a powerful, high-level programming language that supports OOP. It has a simple and easy-to-learn syntax and is widely used in various fields such as web development, data science, and machine learning. Understanding OOP in Python is essential for developing large, complex, and maintainable code. In this blog post, we will go over the basics of OOP in Python, including classes, objects, inheritance, and polymorphism.

What is OOP in Python?

OOP is a programming paradigm that is based on the concept of “objects”, which can contain data and code that manipulates that data. Objects are instances of classes, which are templates for creating objects. Classes define the properties and behaviors of objects. In Python, everything is an object, including variables, functions, and modules.

Python OOP — Object Oriented Programming for Beginners

Defining a class in Python

A class is a blueprint for creating objects. It defines the properties and behaviors of an object. In Python, a class is defined using the class keyword, followed by the name of the class. The properties of a class are defined as variables within the class and the behaviors of the class are defined as methods within the class.

class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
def bark(self):
print("Woof woof!")

In this example, we have defined a class called Dog. The class has two properties: name and age. It also has a method called bark which makes the dog bark.

Creating an object from a class

Once a class is defined, you can use it to create objects. To create an object from a class, you use the class name followed by parentheses.

dog1 = Dog("Fido", 5)
dog2 = Dog("Buddy", 3)

In this example, we have created two Dog objects, dog1 and dog2, each with their own properties.

Accessing class properties

You can access the properties of an object by using dot notation.

print(dog1.name) # Output: Fido
print(dog1.age) # Output: 5

Calling class methods

You can call the methods of an object using dot notation followed by the method name and parentheses.

dog1.bark() # Output: Woof woof!

Inheritance

Inheritance is a mechanism that allows a new class to inherit properties and methods from an existing class. The new class is called the derived class or child class, and the existing class is called the base class or parent class. In Python, inheritance is defined using the parent class name followed by the derived class in parentheses.

class GoldenRetriever(Dog):
def fetch(self):
print("Fetching...")

In this example, we have defined a new class called GoldenRetriever which inherits from the Dog class. The GoldenRetriever class has a new method called fetch which is not present in the Dog class.

Polymorphism

Polymorphism is the ability of an object to take on many forms. In Python, polymorphism is achieved through inheritance and the use of the same method name in different classes. For example, a class Dog and a class Cat can both have a method called speak, but the method in the Dog class will make the dog bark, while the method in the Cat class will make the cat meow.

class Cat:
def __init__(self, name, age):
self.name = name
self.age = age
def speak(self):
print("Meow!")

cat1 = Cat("Whiskers", 2)
cat1.speak() # Output: "Meow!"

Overriding methods

Inherited methods can also be overridden in the derived class to give them new functionality. For example, you can override the bark method in the Dog class to make the dog bark differently.

class Poodle(Dog):
def bark(self):
print("Yip yip!")

poodle1 = Poodle("Sparky", 4)
poodle1.bark() # Output: "Yip yip!"

The self parameter

The self parameter is a reference to the current instance of the class, and is used to access the properties and methods of the class. In Python, the self parameter is automatically passed to the methods when they are called, but you can also pass it explicitly.

class Cat:
def __init__(self, name, age):
self.name = name
self.age = age
def speak(self):
print(f"My name is {self.name} and I am {self.age} years old.")

cat1 = Cat("Whiskers", 2)
cat1.speak() # Output: "My name is Whiskers and I am 2 years old."

The init method

The init method is a special method that is called when an object is created from a class. It is used to initialize the properties of the object. In Python, the init method is defined using the keyword def and the name init.

class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
def bark(self):
print("Woof woof!")

dog1 = Dog("Fido", 5)

Conclusion:

OOP is a powerful programming paradigm that is widely used in Python. Understanding the concepts of classes, objects, inheritance, and polymorphism can help you write clean, maintainable, and efficient code.

Additionally, understanding the special methods such as init and self, and the use of dot notation can make you more proficient in working with OOP in Python. With the knowledge of OOP, you can design and implement complex programs and data structures in an organized and elegant way.

If you liked this article, don’t forget to leave a clap and follow for more articles like this!

--

--