A class is a user-defined blueprint or prototype from which objects are created. An object is a collection of properties (variables) and behavior (methods). The class provides a means to group data and functionality together. Creating a new class creates a new type of object, allowing new instances of that type to be made.
Note : To view clear image, please open in new tab. Place the cursor on image then right click and select open in new tab.
Q.) Why do we need classes and objects in python?
Let’s say you want to track the number of Fans that may have different properties (attributes) like cost, color, number of blades, etc, and methods like fan rotate, switch on/off, etc. Suppose you are a manufacturer of Fan and you manufactured 1000 number of different Fan and you want to note each data about fan like fan color, fan cost, fan’s blade, fan rotation, type of fan, etc. Here you have to create 1000 different entries for Fan and this way is not efficient. It will take you a lot of time to do this.
So Object-Oriented Programming helps us to do these things efficiently. In OOPs, it provides features to make one group of objects and place their properties and methods together and use them.
Q. ) How to create class in python?
python has a keyword named “class“. We use this keyword to create a new class as shown below.
Q. ) What does a class contains in python?
Class have properties/ attributes and methods/ functions of the object.
For example, Suppose Fan is an object so we will create a class with class_name Fan. The fan has some properties and Functions. Properties of Fan are Color, Brand Name, Cost, Number of Blades, types of Fan and Functions of Fan are rotation, switch on/ Off.
Q. ) Where we declare the properties and method of class?
We define the properties of class inside one method and that method is known as “__init__()” (double underscore before init and after. All the properties or attributes should be declared inside “__init__()“. These properties are technically called Object Variables or Instance variables. Methods/ Functions are declared inside a class.
Q. ) What is Object Variables/ Instance Variables?
Variables that are declared within the “__init__()” methods are referred to as Object Variables or Instance Variables. All the properties of an object must be declared within the method “__init__()“.
Q. ) How classes executes in python?
After creating a class you have to create the object of that class to use the corresponding properties and method of a class.
Example : Creation of class with instance variable in python
In the above example, you can see we are creating a class with keyword class and the name of the class is “Fan” after this we are creating one method which is one of the predefined methods in python “__init__()” (more detail about __init__() will be in upcoming blogs so until do not break your head as of now consider it as a method) with one argument “self” (you will know about “self” in the upcoming blogs. until do not break your head). Inside “__init__(self)” we define some properties of Fan like its color, brand, and a number of blades with their corresponding values.
Example : class with instance variable and method in python
In the above example, you can see there are two methods that represent the behavior of an object. The first method is “switch_On” and the second method is “switch_Off”.
Example : Creating the objects in python
In the above example, you can notice the name of the class is the same when creating objects. This is an important point that object name and class name should be the same. F1 is a reference variable of an object Fan(). Without creating an object you can’t use class properties and methods.