Python Objects
An Object an instance of a Class. A class is like a blueprint while an instance is a copy of the class with actual values. Python is an object-oriented programming language that stresses objects.
Importance of creating Objects
They allow us to model real-world entities in code, making programs more modular
They are reusable, and easier to understand.
Class Definition:
A class is a blueprint for creating objects. It defines the attributes (data) and methods (behavior) that objects of that class will have.
class Car:
pass
Method Definition:
Methods are functions defined inside a class. They define the behavior of objects.
class Car:
def drive(self):
print("The car is driving.")
Instantiations:
Creating an object from a class is called instantiation.
my_car = Car()
Executing the Methods:
my_car.drive() # Output: The car is driving.
The self
Keyword:
The self
keyword refers to the current instance of the class. It’s used to access attributes and methods within the class.
class Car:
def __init__(self, color):
self.color = color
def drive(self):
print(f"The {self.color} car is driving.")
Constructors:
A constructor is a special method called __init__
that initializes an object’s attributes when it’s created.
Non-Parameterized Constructors:
class Car:
def __init__(self):
self.color = "red"
Parameterized Constructor:
class Car:
def __init__(self, color):
self.color = color
Inheritance:
Inheritance allows a class (called the child class) to inherit attributes and methods from another class (called the parent class). This promotes code reuse and makes programs more modular.
Parent class in Python
Parent Class: The class being inherited from.
Child Class: The class that inherits from the parent class.
class Vehicle: # Parent class def __init__(self, color): self.color = color def drive(self): print(f"The {self.color} vehicle is driving.") class Car(Vehicle): # Child class def honk(self): print("Beep beep!")
Imagine a diamond-shaped inheritance structure with classes A
, B
, C
, and D
:
A is the parent of B and C.
D inherits from both B and C.
This is called diamond inheritance, and Python uses the Method Resolution Order (MRO) to resolve conflicts.
Method Overriding
Method overriding occurs when a child class provides a specific implementation of a method that is already defined in its parent class.
class Vehicle:
def drive(self):
print("The vehicle is driving.")
class Car(Vehicle):
def drive(self):
print("The car is driving fast.")
Method Resolution Order (MRO)
MRO determines the order in which Python looks for methods in a class hierarchy. You can view the MRO using the mro()
method.
class A:
pass
class B(A):
pass
class C(A):
pass
class D(B, C):
pass
print(D.mro())
# Output: [<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.A'>, <class 'object'>]
Conclusions
Object creation and inheritance are fundamental concepts in Python’s OOP paradigm. By defining classes, creating objects, and using inheritance, you can write modular, reusable, and efficient code. Whether you’re modeling real-world entities or building complex systems, OOP provides the tools you need to succeed.