Sitemap
Towards Dev

A publication for sharing projects, ideas, codes, and new theories.

Object Oriented Programming in Python

--

Object-Oriented Programming (OOP) is a programming paradigm that is based on the concept of objects, which can contain data and functions that operate on that data. OOP is a fundamental concept in Python, and is used extensively in the development of applications, both small and large.

Press enter or click to view image in full size
Photo by Markus Spiske on Unsplash

In this article, we will go through a step-by-step guide to understanding and working with OOP in Python.

Step 1: Understanding Classes and Objects

The most basic building block of OOP in Python is the class. A class is a blueprint for creating objects, which are instances of the class. Each object created from a class has its own set of data and functions, but they are all based on the same class definition.

For example, let’s consider a class “Person”. A person has a name and an age. We can define this class as follows:

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

The __init__ function is a special function in Python classes that is called when an object is created from the class. The self parameter refers to the object being created, and it is used to store the data associated with the object.

Step 2: Defining Instance Methods

In addition to data, objects can also have functions, known as methods. These methods can be used to operate on the data stored in the object. For example, let’s add a method greet to the Person class that prints a greeting message:

rubyCopy code
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
    def greet(self):
print("Hello, my name is", self.name)

Step 3: Creating Objects

Get Pratik Asija’s stories in your inbox

Join Medium for free to get updates from this writer.

Once we have defined a class, we can create objects from it. To create an object, we simply call the class as if it were a function:

makefileCopy code
person = Person("John Doe", 30)

This creates a new Person object, and stores it in the person variable. We can access the data and functions of the object using the dot notation:

pythonCopy code
print(person.name) # Output: John Doe
print(person.age) # Output: 30
person.greet() # Output: Hello, my name is John Doe

Step 4: Inheritance

One of the key benefits of OOP is the ability to create new classes that are based on existing classes. This is known as inheritance, and it allows you to create new classes that have all the properties and methods of the base class, but with additional or modified functionality.

For example, let’s create a new class Student that inherits from the Person class:

rubyCopy code
class Student(Person):
def __init__(self, name, age, student_id):
super().__init__(name, age)
self.student_id = student_id

The super() function is used to call the __init__ method of the base class, ensuring that the data is correctly initialized. The Student class now has all the properties and methods of the Person class, plus the additional student_id property.

Step 5: Polymorphism

Polymorphism is the ability of a class to take on multiple forms. In OOP, this means that a single method can have different implementations in different classes, depending on the data being operated on. For example, let’s consider a method info that prints information about a person or student:

pythonCopy code
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
    def info(self):
print("Name:", self.name)
print("Age:", self.age)
class Student(Person):
def __init__(self, name, age, student_id):
super().__init__(name, age)
self.student_id = student_id
def info(self):
super().info()
print("Student ID:", self.student_id)

In this example, both the Person and Student classes have an info method, but the implementation is different depending on the type of object. When we call the info method on a Person object, it prints the name and age, while when we call the info method on a Student object, it also prints the student ID. This is an example of polymorphism in action.

In conclusion, OOP is a powerful programming paradigm that allows you to model real-world objects and relationships in your code. By using classes, inheritance, and polymorphism, you can create robust and reusable code that is easy to maintain and extend.

--

--

Pratik Asija
Pratik Asija

Written by Pratik Asija

I am a Software Engineer by profession. I am a technology enthusiast and love learning new stuff. https://www.linkedin.com/in/pratik-asija-73b16a136/

Towards Dev
Towards Dev

Published in Towards Dev

A publication for sharing projects, ideas, codes, and new theories.