Python classes- Blueprint of objects creation!
You all know that python is an object-oriented programming language that means it is bound to fields and processes.
For the creation of objects, you need a constructor. This constructor is called classes in the python.
The constructor starts the classes. The __init__ method initialized the class in python.
Basically what the classes do?
Classes hold the functionality and data together.
That functionality and data sometimes called methods.
In this blog, you are going to see:-
• Creation of a class
• Creation of object
• The __init__ method
• self keyword
• Objects method
• Modify object
• Delete object
• Delete objects
• The pass statement
Creation of a class
You can start class with a keyword class. The class would have a name.
Here, you can see that I have created a class school.
The class school has assigned a student name.
Creation of objects
You can create an object with class. And assign a value to it.
Here, you can see that I have created an object student and access the value of the object using dot(.) Operator.
Remember, the dot(.) Operator access the value of the object.
The __init__ method
The __init__ method in python initialized the classes in python.
Here, you can see that I have Initialized the class with __init__method.
The self keyword
In above example you had seen that I have used self keyword.
The self keyword access the current objects which belong to a class.
It doesn’t have to be self. You can call whatever you like. But it should be the first parameter in the class
You can use schoolobject in the place of the self keyword.
Remember, self keyword-only access the class objects.
What is a method in a python class?
A method is a function that belongs to the object.
It’s a function that exposes the attributes of objects in the class.
Here, you can see that I have created a method myname to display the statement.
The method myname belongs to the object student.
Modify objects properties
You can modify objects properties with the help of assignments. Suppose, you have to change the name of the student.
In the above example, you can see that I have modified the student name steve to woz.
With the help of the assignment, you can modify the value of the object and property.
It saves our time to creating new objects.
You can modify the object’s properties as many times as you want.
In the above example, you can see that I have modified the name of the student two times.
So, you can also modify the name of the student as many times as you want.
Delete object properties
Suppose, you want to delete the object’s properties. You don’t want anymore these properties.
What you will do?
You can use the del keyword with the object name.
In the above example, you can see that before using the del keyword. The student name has printed.
After using the del keyword it showing AttributeError because of deletion of an object property.
Remember, I have deleted the object property not the object. Object still available in the output.
In the next section, you are going to see how to delete objects completely.
In this section, you are going to see how to delete objects completely in class.
In the above example, you can see that NameError has come because the object has deleted.
So, with the help of the del keyword, you can delete the objects also.
In the above two examples, you had seen how can you delete both objects and their properties with the help of the del keyword.
The pass statement
You can remain the class definition empty. You had t put some data on it.
But suppose you want not to display class data. Then what you will do?
You can use the pass statement.
In the above example, you can see that in output there come a statement.
This statement shows that there is no output because there is no input.
In this blog, you had seen that class bundling the data and functionality together.
The initialization of class starting from __init__ method. It holds the parameter self.
The self keyword access variables that belong to the class.
You can delete the objects and their properties with the help of the del keyword.
The pass statement in the python class doesn’t give the error.
With the help of a pass statement, you can use the data in next time in the program.
I hope you loved this blog. If you found something misinform you can comment on it. I will fix it.
For more, you can visit:-
"Quality brings clarity"
- A new way to format the string in Python 3.6: fstring - October 11, 2021
- Ultimate new guide for python beginners in 2021 - October 3, 2021
- How to download famous thonny easily for python? - September 20, 2021