Object Oriented Programming (OOP)
Principles of OOP:
◾ Abstraction:
Hiding the complex implementation details and exposing only the essential features of an object.
◾ Encapsulation:
Wrapping data and methods within a class, restricting direct access to some components.
◾ Inheritance:
A mechanism to create a new class based on an existing class to reuse its properties and methods.
◾ Polymorphism:
Allowing objects of different classes to be treated as objects of a common superclass, enabling method overriding and dynamic behaviour.
1. Classes and Objects
◾ Defining classes with class
◾ Creating objects (instances of a class)
◾ Attributes:
- Instance Attributes: Attributes specific to an object.
- Class Attributes: Shared across all instances of a class.
◾ Methods:
- Defining methods with def → Functions defined inside a class that operates on the class attributes.
- Using self to access instance attributes
◾ Accessing and modifying objects
◾ Constructors and Initializers
- Using the
__init__
method - Initializing instance attributes
- Setting default and custom parameters
2. Inheritance
◾ Single Inheritance
◾ Multiple Inheritance
◾ Overriding methods
◾ Using super() to call parent class methods (super().__init__
)
3. Polymorphism
◾ Method overriding for different behaviors in child classes
◾ Defining multiple behaviors for a method
4. Abstract Classes and Interfaces
◾ Concept of abstract classes
◾ Using the abc module
◾ Defining abstract methods
◾ Creating and enforcing interfaces
5. Encapsulation
◾ Access Modifiers:
- Public
- Protected
- Private
◾ Using Getters and Setters for private attributes
◾ Property decorators (@property) for better encapsulation
◾ Descriptor using get, set, delete, and set_name
6. Class and Static Methods
◾ Defining Class Methods with @classmethod
◾ Defining Static Methods with @staticmethod
◾ Difference between Instance Methods, Class Methods, and Static Methods
◾ Using cls for class-related operations
7. Magic Methods, Operator Overloading & dataclass
◾ Understanding magic methods (Dunder methods)
◾ Key Magic Methods:
__init__
,__str__
,__repr__
- Comparison methods (
__eq__
,__lt__
, etc.) - Arithmetic operation methods (
__add__
,__sub__
, etc.) - Item access methods:
__getitem__
,__setitem__
- Customizing object behavior with magic methods
8. Decorator
E-mail: Reza.Saadatyar@outlook.com