Python, like other general-purpose programming languages, has always been an object-oriented language since its inception. You can develop applications using an object-oriented approach. Object-oriented programming (OOPs) is a programming paradigm that uses objects and classes in programming. It is intended to implement real-world entities such as inheritance, polymorphism, and encapsulation in programming. The main concept of OOPs is to tie together data and functions that operate on it together as a single entity so that no other part of the code can access that data. It's not a difficult concept. It's just another way of writing the same code. If you learn the OOPs concepts correctly, you will feel like writing a story or writing a research paper. In general, people say that OOPs is complicated, but it's all about understanding. As mentioned earlier, in the case of OOPs concepts, it's a way of writing code in an object-oriented approach.
Writing code in an object-oriented approach is not mandatory when using Python, but it is always better to write code using classes and objects. So when working on real-time projects, you have to use an object-oriented approach as scripting doesn't work. No matter how good you are at Python, if you say you don't know OOPs, that means you're dead, no one will even let you work on a real project at all. I'm not talking about Kaggle or dummy projects. I'm just talking about the real project.
Let’s try to understand What is OOPs and how we are going to use them. Whenever we try to think about the OOPs concept, in the very first place, classes and objects come to mind first. So what is the meaning of class and object? Let's try to understand this. A class is just a classification of a data set. In other words, they are blueprints rather than actual entities. A Class is something that never exists. We will be able to imagine or get some idea about it, but we cannot visualize or touch it. By definition, classes are the main feature of object-oriented programming, a structure for representing an object and the operations that can be performed on it. For example, let's say you asked me to buy you a laptop. I don't know which laptop to buy. But when I said I was buying a Dell i7 laptop, it gave you an accurate picture of things. So a laptop is something that never existed, but when we talk about a Dell i7 laptop, it is just an object or variable of the laptop that we can visualize and get certain information about the laptop. Thus, real entities are called objects, and classes are nothing more than taxonomies, blueprints, or conceptualizations of entities that exist on this planet. Similarly, int, string, float, list, tuple, etc. are just classes that someone has created. This allows you to create integer, string, float, list, and tuple variables and access built-in functions from them. Similarly, you can create your own classes, your own objects, and your own models, and call and reuse them as needed. Now you can create a clean separation between your code and this is where the concept of OOPs comes into play.
Understanding of Class and Object:
- When creating your own classes, always start with the reserved keyword "class" followed by the class name. The class name can be anything. There are no further restrictions, but you should follow certain naming conventions when trying to name your classes in a real project.
To create an empty class in Python, you don't need to specify a class body, just use the "pass" keyword.
- You can create your own variables and methods within the class.
- Whenever we are trying to create any function going forward in our entire life inside a class, we are supposed to pass a pointer. This simply means trying to map the entire function to the class. This is just a notation or a provision that we should follow.
- We must use the very first argument as a pointer for any function that we are going to create inside a class and in general noted as “self”.
- “self” is not a keyword at all. It’s just a pointer. This is similar to pointers in C++ and references in Java.
- If we have a method inside a class that takes no arguments, then we still must have one argument. That is called “self”.
- A predefined function named which acts like a constructor is used for initialization inside a class. Implementations wise it is different in every language like C++ and Java. According to the OOPs concept, the constructor is a concept or a way by which we will be able to assign data to classes.
- To access anything (functions, variables, etc.) in a class, you must first create an object/variable/instance of the class. Then you can use that object to access anything from that class.
- When we are trying to create an object and we defined the method inside the class, by default the init method will be called. Regardless of the nomenclature or whatever parameters that we mention, we are supposed to pass that parameter when creating class variables.
- You can create multiple objects of one class and one more thing is that the variable name and method name shouldn’t be the same.
Let’s understand this by using an example:
Let’s see what happens when we print the first_class object, i.e. classobj :
Now we have a first_class object at 0x0000023EDB882640. This weird string of letters and numbers is the memory address where the first_class object is stored in your computer's memory. Note that the memory address displayed on your screen is different. Also, even if you create many objects for the same class, each instance will have a different memory address. A built-in function called "__str__()"is available to get valuable information about calling or printing an object/variable/instance. Here we are trying to override this function. That means you have to provide your own information about the object. Let’s see another example for a better understanding:
The __init__() and __str__() are called Dunder methods because they start and end with a double underscore. There are many Dunder methods available that can be used to customize classes in Python.
There is a concept called Protection of Abstraction or Access Specifier which defines how to access the members (attributes and methods) of a class. Abstraction is nothing but a skeleton. It’s a big concept, you will be able to get multiple definitions associated with inheritance, polymorphism, abstraction itself, and even associated Access Specifier. Most programming languages have three types of access specifiers within a class: public, protected, and private. Python uses the “_” symbol to specify access control for a particular data or function of a class.
So in the next article, we will discuss about three more important concepts Inheritance, Polymorphism, and Encapsulation.