Yes, Python is an object-oriented programming (OOP) language, but it is also a multi-paradigm language

 Yes, Python is an object-oriented programming (OOP) language, but it is also a multi-paradigm language. This means that Python supports multiple programming paradigms, including:

  1. Object-Oriented Programming (OOP) – Python supports classes, objects, inheritance, polymorphism, and encapsulation.
  2. Procedural Programming – You can write Python code using functions without using objects or classes.
  3. Functional Programming – Python allows the use of higher-order functions, lambda functions, and concepts like map, filter, and reduce.

Why is Python considered an Object-Oriented Language?

  • It allows defining classes and creating objects.
  • Supports encapsulation, inheritance, and polymorphism.
  • Everything in Python (even functions and modules) is an object.

Example of OOP in Python:

python

class Animal: def __init__(self, name): self.name = name # Attribute def speak(self): return "Some sound" class Dog(Animal): def speak(self): return "Woof!" dog = Dog("Buddy") print(dog.name) # Output: Buddy print(dog.speak()) # Output: Woof!


Is Python a "pure" OOP language?

No, because it allows procedural and functional programming styles. In contrast, languages like Java and C++ enforce OOP more strictly.

Great! Let's dive into Object-Oriented Programming (OOP) in Python step by step.


πŸ”Ή Core OOP Concepts in Python

Python supports the following four fundamental OOP principles:

1️⃣ Class & Object – Blueprint & real-world instance
2️⃣ Encapsulation – Data hiding using access specifiers
3️⃣ Inheritance – Reusing code by extending a class
4️⃣ Polymorphism – Same interface, different behavior


1️⃣ Classes & Objects πŸ“Œ

A class is a blueprint for creating objects, while an object is an instance of a class.

πŸ“ Example: Creating a Class and Object

python
class Car: def __init__(self, brand, model): # Constructor self.brand = brand # Attribute self.model = model # Attribute def show_details(self): return f"Car: {self.brand} {self.model}" # Creating an object car1 = Car("Toyota", "Camry") print(car1.show_details()) # Output: Car: Toyota Camry

✅ Here, Car is the class, and car1 is an object.


2️⃣ Encapsulation πŸ”’

Encapsulation is about restricting direct access to some parts of an object to protect the data.
Python uses private variables (prefix with _ or __) for encapsulation.

πŸ“ Example: Private Variables

python

class BankAccount: def __init__(self, balance): self.__balance = balance # Private variable 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 # print(account.__balance) # ❌ Error: Cannot access private variable

✅ Using __balance, we prevent direct access to the balance.


3️⃣ Inheritance πŸ‘¨‍πŸ‘©‍πŸ‘§

Inheritance allows a class (child) to inherit attributes and methods from another class (parent).
This promotes code reusability.

πŸ“ Example: Inheritance in Python

python
class Animal: def __init__(self, name): self.name = name def speak(self): return "I make a sound" # Dog class inherits from Animal class Dog(Animal): def speak(self): return "Woof!" # Cat class inherits from Animal class Cat(Animal): def speak(self): return "Meow!" dog = Dog("Buddy") cat = Cat("Whiskers") print(dog.speak()) # Output: Woof! print(cat.speak()) # Output: Meow!

Dog and Cat classes reuse the code from the Animal class.


4️⃣ Polymorphism 🎭

Polymorphism allows methods to have the same name but behave differently.

πŸ“ Example: Method Overriding

python

class Bird: def fly(self): return "Some birds can fly" class Sparrow(Bird): def fly(self): return "Sparrow flies high" class Penguin(Bird): def fly(self): return "Penguins cannot fly" # Objects sparrow = Sparrow() penguin = Penguin() print(sparrow.fly()) # Output: Sparrow flies high print(penguin.fly()) # Output: Penguins cannot fly

✅ The fly() method behaves differently in each subclass.


πŸ”₯ Advanced OOP Features in Python

1️⃣ Multiple Inheritance (A class inheriting from multiple parent classes)

python
class A: def method_A(self): return "Method from A" class B: def method_B(self): return "Method from B" class C(A, B): # Multiple Inheritance pass obj = C() print(obj.method_A()) # Output: Method from A print(obj.method_B()) # Output: Method from B

2️⃣ Abstract Classes (Enforcing method implementation in subclasses)

python

from abc import ABC, abstractmethod class Vehicle(ABC): @abstractmethod def start_engine(self): pass class Car(Vehicle): def start_engine(self): return "Car engine started" # vehicle = Vehicle() # ❌ Error: Can't instantiate abstract class car = Car() print(car.start_engine()) # Output: Car engine started

πŸš€ Summary

  • Class & Object → Blueprint & real-world instance
  • Encapsulation → Hiding data using private variables
  • Inheritance → Child class inherits from parent class
  • Polymorphism → Same method, different behavior

Comments

Popular posts from this blog

Ecommerce website

Your task is to find the missing number.