Object-Oriented Programming (OOP) is a powerful paradigm that organizes code around objects—instances of classes that encapsulate both data and behavior. Python, being an object-oriented language, provides a simple yet efficient way to implement OOP principles, making it easier to structure and manage complex programs.
In this blog, we’ll explore key OOP concepts like classes, objects, methods, instance variables, and constructors, along with practical examples. Whether you’re a beginner or looking to deepen your understanding, this guide will help you harness the power of OOP in Python for better code organization and reusability.
Object-Oriented Programming (OOP) is a paradigm that emphasizes organizing code around “objects.” These objects are instances of classes and can contain both data (attributes) and functions (methods) to manipulate that data. Python, as a versatile and powerful programming language, supports OOP and provides a simple yet effective way to work with classes and objects.
In this post, we’ll explore key concepts such as classes, objects, methods, and constructors within the context of Python, with examples to illustrate how OOP principles can be applied.
Understanding a Class
A class in Python is like a blueprint or a template for creating objects. It defines the structure that objects created from the class will have, including the attributes (data) and methods (functions) that they will possess.
What Is an Object?
An object is a specific instance of a class. Every object has its own unique data but can share methods defined in the class. Essentially, an object is a runtime entity that is instantiated based on the class structure.
Example:
class Example:
a = 10 # Class-level variable
# Create an instance (object) of the class
obj = Example()
print(obj.a) # Output: 10
In this example, obj is an object of the Example class, and we can access the class attribute a using the object.
Member Functions:Functions Inside Classes
In OOP, member functions (or methods) are functions that are defined inside a class. These functions operate on the class’s data and can be called on instances (objects) of the class.
For example:
class Sample:
x = 100 # Class-level variable
def display(self):
print(f”Class Variable Value: {self.x}”)
# Create an object of the class
obj = Sample()
obj.display() # Output: Class Variable Value: 100
In this case, display is a method that prints the value of the class variable x.
Instance Variables:
Data Specific to Each Object
An instance variable is a variable that belongs to an individual instance of the class. Each object can have its own version of instance variables, which are defined inside methods using the self keyword.
class Sample:
x = 100 # Class variable shared by all objects
def set_value(self, value):
self.q = value # Instance variable
def display(self):
print(f”Class Variable: {self.x}”)
print(f”Instance Variable: {self.q}”)
# Create an object and set instance-specific value
obj = Sample()
obj.set_value(50)
obj.display()
In this example, q is an instance variable unique to each object, whereas x is a shared class variable.
Constructors: Special Methods for Initialization
A constructor in Python is a special method used to initialize objects. Python uses __init__ as the constructor method. It is automatically called when a new object is created from a class.
Constructors can be of two types:
1. Default Constructor: Initializes attributes with default values.
2. Parameterized Constructor: Accepts parameters to initialize attributes with custom values.
Default Constructor Example:
class Sample:
def __init__(self):
self.q = 10 # Default value
def display(self):
print(f”Instance Variable: {self.q}”)
# Create an object of the class
obj = Sample()
obj.display()
Parameterized Constructor Example:
class Sample:
def __init__(self, value):
self.q = value # Initialize with given value
def display(self):
print(f”Instance Variable: {self.q}”)
# Create an object with a specific value
obj = Sample(20)
obj.display()
Example 1: Circle Class for Calculating Area and Circumference
Let’s create a Circle class where we can input the radius and calculate the area and circumference using methods.
class Circle:
def __init__(self):
self.radius = float(input(“Enter the radius of the circle: “))
def calculate_area(self):
area = 3.14 * self.radius ** 2
print(f”Area of Circle: {area}”)
def calculate_circumference(self):
circumference = 2 * 3.14 * self.radius
print(f”Circumference of Circle: {circumference}”)
# Create a Circle object and compute its area and circumference
circle = Circle()
circle.calculate_area()
circle.calculate_circumference()
Example 2: Student Class for Storing Student Information
Now let’s design a Student class that accepts a student’s name, roll number, age, and marks. It will have methods to assign the age and marks, and a method to display all student information.
class Student:
def __init__(self):
self.name = input(“Enter student’s name: “)
self.roll_number = int(input(“Enter student’s roll number: “))
self.age = None
self.marks = None
def set_age(self):
self.age = int(input(“Enter student’s age: “))
def set_marks(self):
self.marks = int(input(“Enter student’s marks: “))
def display(self):
print(f”Student Name: {self.name}”)
print(f”Roll Number: {self.roll_number}”)
print(f”Age: {self.age}”)
print(f”Marks: {self.marks}”)
# Create a Student object and display information
student = Student()
student.set_age()
student.set_marks()
student.display()
Indian Institute of Embedded Systems – IIES