Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Is Python Object Oriented or Procedural?
Python is a multi-paradigm programming language that supports both Object-Oriented Programming (OOP) and Procedural Programming. As a high-level, general-purpose language, Python allows developers to choose the programming style that best fits their needs.
You can write programs that are largely procedural, object-oriented, or even functional in Python. The flexibility to switch between paradigms makes Python suitable for various applications, from simple scripts to complex enterprise systems.
Object-Oriented Programming in Python
Python supports all core OOP concepts including classes, objects, encapsulation, inheritance, and polymorphism. Here's an example demonstrating OOP principles ?
class Rectangle:
def __init__(self, length, breadth, unit_cost=0):
self.length = length
self.breadth = breadth
self.unit_cost = unit_cost
def get_perimeter(self):
return 2 * (self.length + self.breadth)
def get_area(self):
return self.length * self.breadth
def calculate_cost(self):
area = self.get_area()
return area * self.unit_cost
# Create a Rectangle object
# breadth = 120 cm, length = 160 cm, 1 cm^2 = Rs 2000
rectangle = Rectangle(160, 120, 2000)
print("Area of Rectangle: %s cm^2" % (rectangle.get_area()))
print("Cost of rectangular field: Rs. %s" % (rectangle.calculate_cost()))
Area of Rectangle: 19200 cm^2 Cost of rectangular field: Rs. 38400000
Procedural Programming in Python
Python also supports procedural programming using functions, loops, and control structures. Here's the same rectangle calculation using a procedural approach ?
def get_area(length, breadth):
return length * breadth
def get_perimeter(length, breadth):
return 2 * (length + breadth)
def calculate_cost(length, breadth, unit_cost):
area = get_area(length, breadth)
return area * unit_cost
# Using procedural approach
length = 160
breadth = 120
unit_cost = 2000
area = get_area(length, breadth)
cost = calculate_cost(length, breadth, unit_cost)
print("Area of Rectangle: %s cm^2" % area)
print("Cost of rectangular field: Rs. %s" % cost)
Area of Rectangle: 19200 cm^2 Cost of rectangular field: Rs. 38400000
Comparison
| Aspect | Object-Oriented | Procedural |
|---|---|---|
| Code Organization | Classes and methods | Functions and modules |
| Data Handling | Encapsulated in objects | Passed as parameters |
| Best For | Complex systems, reusability | Simple scripts, linear logic |
Conclusion
Python's multi-paradigm nature allows you to choose between object-oriented and procedural programming based on your project requirements. Use OOP for complex applications requiring data encapsulation and inheritance, or procedural programming for simpler, linear tasks.
