Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python
Object-oriented programming (OOP) is a paradigm that uses “objects” to design applications and computer programs. Utilizing the power of classes and objects can make software development more efficient and manageable. Python, known for its simplicity and readability, is an excellent language for implementing OOP concepts.
This article delves into a Python program that illustrates basic OOP principles, showcasing how classes and objects operate in Python.
Topic Explanation
Understanding the Python Class Structure
At the heart of OOP in Python lies the class, a blueprint for creating objects. In our program, MyClass is defined as a class. It encapsulates characteristics (attributes) and behaviors (methods) that the objects of this class will have. The __init__ method, known as the constructor, is automatically called when a new object of the class is created. It initializes the object’s attributes. In MyClass, __init__ initializes two attributes, a and b, with values 10 and 20, respectively.
The significance of __init__ cannot be overstated. It allows for the creation of objects with initial states, ensuring that each object starts with a specific configuration. This is crucial in building applications where objects must have a defined state upon their creation.
Dynamic Nature of Python Objects
Python objects are dynamic. This means they can have attributes added or removed at runtime. In the given program, after creating an instance M1 of MyClass, the method display is called, which further adds another attribute c to M1. Additionally, the program demonstrates the dynamic nature of Python objects by directly adding a new attribute d to M1 outside the class definition. This flexibility is a powerful feature of Python, enabling programmers to modify objects as needed during runtime.
When M2, another instance of MyClass, is created, it doesn’t have the attributes c and d, as these were added only to M1. The print statements at the end compare the attributes of M1 and M2, illustrating how different instances of the same class can have different attributes. This dynamism makes Python particularly suitable for applications requiring a high degree of flexibility and adaptability.
Prerequisites
- Basic understanding of Python syntax and operations.
- Familiarity with the concept of classes and objects in programming.
- An installed version of Python on your machine for running and testing the code.
- Basic knowledge of object-oriented programming principles.
Program
Code:
# Defining a class named 'Myclass'
class Myclass:
# Initializer or constructor method
def __init__(self):
self.a = 10 # Initializing instance variable 'a' with value 10
self.b = 20 # Initializing instance variable 'b' with value 20
# Method to modify the state of the object
def display(self):
self.c = 30 # Adding a new instance variable 'c' with value 30
# Creating an instance 'M1' of the class 'Myclass'
M1 = Myclass()
M1.display() # Calling the 'display' method on the instance 'M1'
M1.d = 40 # Adding a new attribute 'd' with value 40 to instance 'M1'
# Creating another instance 'M2' of the class 'Myclass'
M2 = Myclass()
# Printing the dictionary containing all instance variables for 'M1'
print("M1 Object", M1.__dict__)
# Printing the dictionary containing all instance variables for 'M2'
print("M2 Object", M2.__dict__)
Code Explanation
1. A class called Myclass is defined with an __init__() method and a display() method
2. The __init__() method initializes attributes a with value 10 and b with value 20 when a Myclass object is created
3. The display() method defines another attribute c with value 30
4. An object called M1 is created from Myclass. This calls the __init__() method to initialize a and b.
5. The display() method is called on M1. This sets attribute c to 30 in that object.
6. An attribute d with value 40 is set directly on the M1 object.
7. Another object M2 is created from Myclass. This again calls __init__() to initialize its a and b attributes.
8. The __dict__ attributes show the following:
- M1 has attributes a, b, c, and d
- M2 only has attributes a and b initialized by its __init__() call
Summary
This article provided an insight into object-oriented programming in Python through a simple yet illustrative example. It explained the structure and dynamics of Python classes and objects, demonstrating how attributes and methods define the state and behavior of objects.
The dynamic nature of Python objects was highlighted, showing how attributes can be added or modified at runtime. This program serves as a foundational example for understanding how Python implements OOP concepts, making it a valuable resource for beginners in Python programming and OOP.