Course Content
How to Add a Virtual Environment in Python
0/1
Set
0/1
Magic Methods
0/1
Python
About Lesson

Object-Oriented Programming (OOP) is a programming paradigm that uses objects and classes to design and organize software. Python supports OOP, allowing developers to create reusable and modular code.

Key Concepts of OOP

  1. Class
  • A class is a blueprint for creating objects. It defines the properties (attributes) and behaviors (methods) that the objects created from it will have.

Syntax:

Python
class ClassName:
    # Constructor
    def __init__(self, attribute1, attribute2):
        self.attribute1 = attribute1
        self.attribute2 = attribute2

    # Method
    def method_name(self):
        print("This is a method.")

2. Object

  • An object is an instance of a class. It represents a specific entity defined by the class blueprint.

Syntax:

Python
obj = ClassName(value1, value2)
obj.method_name(

Encapsulation

  • Encapsulation refers to bundling the data (attributes) and methods that work on the data into a single unit (class). It also involves restricting access to some of the object’s components.
  • Use underscore _ or double underscore __ for private attributes.

Example:

Python
class Sample:
    def __init__(self):
        self._protected_attribute = "Protected"
        self.__private_attribute = "Private"

    def get_private_attribute(self):
        return self.__private_attribute

obj = Sample()
print(obj.get_private_attribute())