This is a text field

Python classes- Blueprint of objects creation!

Image credit: DataCamp

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
properties
• Delete object
properties
• Delete objects
• The pass statement

Creation of a class

You can start class with a keyword class. The class would have a name.

For example,

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.

For example,

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.

For example,

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

For example,

You can use schoolobject in the place of the self keyword.

Remember, self keyword-only access the class objects.


Objects method

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.

For example,

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.

For example,

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.

For example,

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.

For example,

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.


Delete objects

In this section, you are going to see how to delete objects completely in class.

For example,

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.

For example,

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.


Conclusion:-

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"

Avatar for Vipul kunwar
Follow me

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    Back to top