0% found this document useful (0 votes)
1 views4 pages

Python Oop Detailed

Object-Oriented Programming (OOP) in Python organizes code into classes and objects, facilitating the modeling of real-world concepts. Key principles include encapsulation, inheritance, polymorphism, and abstraction, which enhance code reusability and maintainability. OOP allows for cleaner and more modular code, making it easier to develop robust applications.

Uploaded by

bobbyneupane70
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views4 pages

Python Oop Detailed

Object-Oriented Programming (OOP) in Python organizes code into classes and objects, facilitating the modeling of real-world concepts. Key principles include encapsulation, inheritance, polymorphism, and abstraction, which enhance code reusability and maintainability. OOP allows for cleaner and more modular code, making it easier to develop robust applications.

Uploaded by

bobbyneupane70
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Object-Oriented Programming (OOP) in Python - Detailed Explanation

==================================================================

Object-Oriented Programming (OOP) is a programming paradigm that organizes code into objects
that contain both data and behavior. Python fully supports OOP, making it easy to model real-world
concepts.

1. Classes and Objects


----------------------
- Class: A blueprint for creating objects. It defines attributes (data) and methods (functions) that the
objects will have.
Example:
class Car:
def __init__(self, brand, model):
self.brand = brand
self.model = model

def start(self):
print(f"{self.brand} {self.model} is starting...")

- Object: An instance of a class.


Example:
my_car = Car("Toyota", "Corolla")
my_car.start()

2. Attributes and Methods


-------------------------
- Attributes: Variables that belong to an object.
- Methods: Functions defined inside a class that describe the behavior of the objects.

Example:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age

def greet(self):
print(f"Hello, my name is {self.name} and I am {self.age} years old.")

3. Encapsulation
----------------
Encapsulation means restricting direct access to certain attributes and methods to protect the
object's state.
- Public: Accessible everywhere.
- Protected: Indicated by a single underscore (_), should not be accessed directly outside the class.
- Private: Indicated by double underscore (__), not accessible outside the class.

4. Inheritance
--------------
Inheritance allows one class to inherit attributes and methods from another.
Example:
class Animal:
def speak(self):
print("Animal speaks")

class Dog(Animal):
def speak(self):
print("Woof!")

5. Polymorphism
---------------
Polymorphism allows objects of different classes to be used interchangeably, usually through
methods with the same name but different implementations.

Example:
def animal_sound(animal):
animal.speak()

animal_sound(Dog())

6. Abstraction
--------------
Abstraction hides complex implementation details and shows only the necessary features.
Example:
from abc import ABC, abstractmethod

class Shape(ABC):
@abstractmethod
def area(self):
pass

class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14 * self.radius ** 2

7. Special Methods
------------------
Python classes can define special (magic) methods to customize behavior.
Examples: __init__, __str__, __len__, __getitem__, __setitem__

8. Benefits of OOP
------------------
- Code reusability through inheritance.
- Improved organization by grouping related data and behavior.
- Easier maintenance and scalability.
- Models real-world entities more naturally.
Summary
-------
OOP in Python allows developers to write cleaner, modular, and reusable code. By using classes,
objects, and the four principles of OOP (Encapsulation, Inheritance, Polymorphism, Abstraction),
developers can create robust and maintainable applications.

You might also like