What is Object-Oriented Programming (OOPs) in Python?
Object-oriented programming is the most widely used programming paradigm that enables developers to solve the problems using classes and objects. It reduces development time, helps us in organizing the code, and thus makes it easier for us to reuse and maintain the code. Instead of thinking about the code as a sequence of actions, programmers can now think it as a collection of classes and objects that interact with each other.
OOPs is great for building frameworks and tools. Many popular Python libraries and frameworks such as Django, Flask, Scrapy, etc. leverages the concept of OOPs. Python supports all the four foundational features of the object-oriented programming paradigm which are explained below-
Note: Everything in Python is an object.
- Encapsulation- It is a way of representing the data and operations on data as a single unit. In Python, encapsulation is achieved using classes. A class consists of variables (that hold data) and methods (that holds the logic for operations).
- Abstraction- It is a process of hiding internal working and showing only the essential functionality of the object. It is achieved using classes. For example, A Python developer can use Spider class without knowing how it works.
- Inheritance- It is about reuse of existing code without having to rewrite the code from scratch. A new class can inherit the members of an already existing class and adds its own. The already existing class is called the parent class, superclass or base class and the new class is called the derived class, subclass or derived class. Python supports single-level, multi-level and multiple inheritances.
- Polymorphism- It means one name but many forms. Python does not support constructor and method overloading but with the help of keyword and non-keyword variable-length argument, you can achieve the same effect. However, operator overloading is supported by Python using magic methods.
Following are the topics that are covered in Python OOPs-