Is Python Object Oriented?
Yes, Python is an object-oriented programming (OOP) language, but it is also multi-paradigm, meaning it supports procedural and functional programming styles. Python provides all the essential features of OOP, such as classes, objects, inheritance, polymorphism, encapsulation, and abstraction.
This article explores Python’s object-oriented nature, explaining key concepts and how Python differs from other strictly OOP languages like Java and C++.
1. What is Object-Oriented Programming (OOP)?
Object-Oriented Programming (OOP) is a programming paradigm that organizes code into objects and classes. Objects are instances of classes and contain both data (attributes) and methods (functions) to manipulate that data.
The four pillars of OOP:
- Encapsulation – Restrict direct access to object data.
- Abstraction – Hide complex implementation details.
- Inheritance – Allow a class to derive properties from another class.
- Polymorphism – Use the same interface for different data types.
Python supports all these features, making it an object-oriented language.
2. How Python Implements OOP
2.1 Classes and Objects in Python
A class is a blueprint for creating objects, and an object is an instance of a class.
Example: Creating a Class and Object
pythonCopyEditclass Car:
def __init__(self, brand, model):
self.brand = brand
self.model = model
def display_info(self):
return f"Car: {self.brand} {self.model}"
# Creating an object
my_car = Car("Toyota", "Corolla")
print(my_car.display_info()) # Output: Car: Toyota Corolla
2.2 Encapsulation in Python
Encapsulation restricts direct access to object data and allows controlled modification.
Example: Using Private Attributes
pythonCopyEditclass BankAccount:
def __init__(self, balance):
self.__balance = balance # Private attribute
def deposit(self, amount):
self.__balance += amount
def get_balance(self):
return self.__balance
# Creating an object
account = BankAccount(1000)
account.deposit(500)
print(account.get_balance()) # Output: 1500
Here, __balance
is a private variable, meaning it cannot be accessed directly (account.__balance
will raise an error).
3. Inheritance in Python
Inheritance allows a class to inherit attributes and methods from another class.
Example: Single Inheritance
pythonCopyEditclass Animal:
def make_sound(self):
return "Some sound"
class Dog(Animal): # Dog class inherits from Animal
def make_sound(self):
return "Bark"
dog = Dog()
print(dog.make_sound()) # Output: Bark
Python also supports multiple inheritance and multilevel inheritance.
4. Polymorphism in Python
Polymorphism allows different objects to use the same method in different ways.
Example: Method Overriding
pythonCopyEditclass Bird:
def fly(self):
return "Flying"
class Sparrow(Bird):
def fly(self):
return "Sparrow is flying low"
class Eagle(Bird):
def fly(self):
return "Eagle is soaring high"
birds = [Sparrow(), Eagle()]
for bird in birds:
print(bird.fly())
Output:
csharpCopyEditSparrow is flying low
Eagle is soaring high
Here, both Sparrow
and Eagle
override the fly()
method differently.
5. Is Python Purely Object-Oriented?
Unlike Java, which enforces OOP principles strictly, Python is multi-paradigm. You can write procedural or functional code alongside object-oriented code.
Example: Procedural Programming in Python
pythonCopyEditdef add_numbers(a, b):
return a + b
print(add_numbers(5, 3)) # Output: 8
This is not object-oriented, proving that Python is not a purely OOP language.
6. Key Differences Between Python and Strict OOP Languages
Feature | Python | Java |
---|---|---|
OOP Paradigm | Multi-paradigm (supports procedural and functional) | Strictly OOP |
Everything is an Object | Yes | No (primitives like int , float are not objects) |
Private Variables | No strict enforcement (__var is convention) | Strict enforcement (private keyword) |
Static Typing | No (dynamic typing) | Yes (static typing) |
Multiple Inheritance | Yes | No (only interfaces allowed) |
7. Conclusion
Python supports OOP with classes, objects, inheritance, encapsulation, and polymorphism, but it is not purely object-oriented because it allows procedural and functional programming as well. Its flexibility makes it an excellent choice for various programming tasks.
Python is OOP when you use it that way—but it does not enforce it, making it more adaptable than languages like Java. 🚀