OOP (Object-Oriented Programming) in Python is a programming paradigm that uses objects and classes to structure code in a way that models real-world entities.
OOPs Concepts in Python.
Class in Python
- Objects in Python
- Polymorphism in Python
- Encapsulation in Python
- Inheritance in Python
- Data Abstraction in Python
Classes and Objects:
- Class: A blueprint for creating objects (a particular data structure).
- Object: In Python, every instance of a class is an object. In other words, by defining a class, you are only defining a blueprint for that kind of object, specifying what kind of attribute it would hold and what methods it would have. To create the object from that class is calling the class as if it were a function and initializing a new instance of the class.
Some points on the OOP (Object-Oriented Programming) in Python class:
Classes are created by using the keyword class.
Attributes are the variables that belong to a class.
Attributes are always public and can be accessed using the dot (.) operator. Eg.: Myclass.Myattribute
An object consists of:
- State: It gets represented by attributes of an object. The state also reflects the properties of an object.
- Behavior: It is represented by methods of an object. It also shows the reaction of an object to other objects.
- Identity—provides a unique name to the object and enables one object to take action concerning other objects.
This is the Python self.
- Class methods require an additional first parameter in the method definition. We do not provide a value for that parameter when we call the method; Python supplies it
- If we have a method with no arguments, then in the definition we still need to have at least one argument.
- This corresponds to this pointer in C++ and this reference in Java.
- When we call a method of this object as myobject.method(arg1, arg2), this is automatically converted by Python into MyClass.method(myobject, arg1, arg2)– this is all the special self is about.
The Python __init__ Method
This is comparable to the constructors of both C++ and Java. It is executed immediately an object of the class is instantiated. The method comes in handy to do any initialization you want to do with your object. Now, let us define a class and create some objects using the self and __init__ method.
Encapsulation: This refers to the packaging of data and methods operating on the data in one unit, typically a class. It also involves the restriction of direct access to some components of an object, a way to avoid accidental interference and misuse of the data.
Inheritance:
This is a mechanism where in one class inherits the attributes and methods of another class; it is usually referred to as the child inheriting from the parent or subclass from superclass. Inheritance enables the reusing of code and building a hierarchical relationship between classes.
Polymorphism: Ability to present the same interface for different data types.
Abstraction : Hiding complex implementation details and showing only the necessary features.
Read Also :- Eloquent ORM: Relationships in Laravel
Using Query ID in Elementor for Custom Post Filtering