Classes and objects in Python
As we have seen earlier, that object-oriented programming is one of the approaches followed by python programming language. Now, we are going to get started with understanding what classes and objects are.
If you look around, you can find many real-life objects, like water bottles, mobile phones, computers, pens, notebooks, earphones, etc. these all are objects, and they have certain properties. Now, when we talk about properties, we talk about what are the attributes and the behaviors of the object, like if we consider earphones as objects, we can say that the attributes of earphones can be price, color, the manufacturer, etc. on the other hand, the behaviors would be playing the song, pausing a song, changing song, etc.
Classes and objects
Now, the thing is that pick some random object, and you can find that it has some attributes and behaviors. The thing is that when the objects are created, there must be some blueprint or template, using which those objects are created. Like if we are creating a house, then, first of all, we are presented with a blueprint, showing how the house would look like. But that blueprint is not the real house, right? But using that blueprint, any number of houses can be created, which look like the house in the blueprint.
So, this brings us to the concept of classes and objects. You might be wondering why am I talking about the attributes, behaviors, blueprint, and all that stuff. Well, this is because it connects us to the beautiful concept of classes and objects.
See, when the objects are created, they won’t be just directly created, but they would be created on the basis of some template or blueprint, which would be required to define. So, this brings us to define what the classes are. A class is a blueprint or a template, using which, the objects can be created. It is just a logical entity that has some attributes and behaviors defined that the particular object of this class is going to have.
On the other hand, the object is nothing but an instance of a class. It is the physical entity that is there in the memory. Using the class, we can create many objects, as per the need.
When you would get to see how we can create classes and how we can create objects, things would get clearer. We can also say that the class is something that defines how the object of that class should be, and the object is something that is created using that class. If there is a class earphone, and an earphone (this is an object) is created using the class, we can simply say that it has some attributes and behaviors of its own, and which are limited to it. First of all,
Let’s consider how we can create the classes and objects so that we can move further into the concepts.
Creating a class
Now, that we are quite familiar with what is class, we are going to have a look at how we can create a class. This is a very simple thing to do. First of all, we have to use a keyword, that is ‘class’, followed by some name of the class (we can name the class in a similar way we do with the functions or variables, but we should give some meaningful name, which defines what that class is for), and then a colon. After that, within the indentations, we are going to write the things that we want to include in the class.
So, now is the time to create a simple class. For example, we are going to create an earphones class.
Have a look at the below program, which tries to demonstrate the same thing –
As you can see, we have used the class keyword, and the name of the class is GyaniPandit (You can have any classname according to the requirements). After the colon, within the indents, we have written the pass keyword. This is just done because we cannot keep the class empty. But very soon, we are going to have a look at the different things that we are going to put into our class. But before that, we are going to have a look at how we can create an object of some class.
Creating an object of a class
Creating an object of some class in our python program is so easy. When you would create the object of some class yourselves, you would realize this thing. To create some object, you just have to write something like this –
As you can see, this is pretty much similar to how are we creating sets, dictionaries, lists, etc… You just have to create a reference variable, and then you have to use the class name as can be seen above, and the object will be created.
Also, we have discussed earlier, that an object is going to have some attributes and behaviors. Now, Let’s discuss how are we going to have them for the object of some classes. But even before that, Let’s understand what the attributes and the behaviors are here. Basically, the attribute is nothing but the variables, and the behavior is some method related to the class. In fact, we are already familiar with both, the variables, and the methods as well, but here, we are going to create them for some particular class. So, soon we are going to consider having some attributes and some behaviors for the class.
Let’s consider a practical example of this. Consider that there is a class, Let’s call it Earphones. Now, the class Earphones will be a blueprint for creating the earphones object. If we consider the attributes of an earphone, it can be color, price, model number, company, etc. and on the other hand, earphones have some behaviors like changing the song, playing the song, pausing songs, etc.
Also, as another example, we can consider some MobilePhone classes, which can be considered as a blueprint for creating mobile phone objects. The mobile phone also has some attributes like color, company, origin, model number, price, etc.. and also has some behaviors like switch on, switch off, etc…
So, from such examples, we can understand the attributes and behaviors of the different objects can be, that we are going to define in the class. Let’s have a look at how we can have some attributes and behaviors here. Let’s continue with the example of a Mobile Phone. Let’s first have a look at a very simple program and let’s try to understand the different things that are going to be considered in the program.
As you can see in the above program, we have created a class MobilePhone, using the class keyword, and the class name as MobilePhone. Within the class, we have some methods (the method is just a function, belonging to some class), and the methods are switch_on and switch_off. We can have more, but for the sake of explanation, we would stick with two.
There is one parameter given for both methods, and it says ‘self’. It just basically means the instance of the class. You can write something else than self there, but let’s stick with the convention, and stick with self here. It is the first parameter basically.
After that, you can see that we are creating two objects of the Earphones class, and then we are giving some variables there. You might notice that we are creating some variables, specifying the object reference variable name, followed by the dot, and then followed by the attribute (for example, mobile1.color). This makes sense because the individual objects can have their own attributes, so this color is for the object pointed by the mobile1 reference variable. We have done the same thing for mobile2. After that, we are trying to access the company variable for both the objects, and we find them different, which again makes sense since both the mobiles are from different companies.
As you can see, in the above program, all the other things are the same, but now, we have called the switch_on method for both the MobilePhone objects. So, this was about creating some objects for our class, and we also got an introduction to the attributes and behaviors.
But if you observe in our program, when we are creating the objects, after that, we are giving the values for the attributes. Every time, we are doing this thing manually, it results in lengthy code. So, we can do something better to shorten the code, and give the values to the attributes as well? Well, we have got something for our help, and we are going to now discuss it.