In this tutorial, we will discuss the most Important topics of Python Programming language that are Class and Object. We will also learn how can we create a class and its objects.
Python Class and Objects
What are classes and objects in Python?
Python is an Object-oriented programming language and in Object orientation programming we use class and objects to make a program. To understand OOPs let’s compare it with Procedural Language though Python supports both Procedural as well as Object Oriented programming, but python is more concern and known for its OPP’s concept. In Procedural programming, we mostly divide our code into different functions to make it modular but in OOP’s we divide the program into different classes to give it a modular format.
A class is a collection of attributes and methods (the user-defined function inside a class) which came in existence when the object of that class is created. We can also say that a Class is a blueprint of an object and the object is the implementation of that blueprint.
Create a Class in Python:
To create a user-defined function, we use def keyword and to define a class we use class keyword, and the class keyword is followed by the class name.
As a function, we can also use triple quotes inside a class so it could have a docstring which contain additional information of the class.
”’ class docstring”’
# Class block
Example create a Class:
class My_class: class_attribute_1 = 40 class_attribute_2 = "Hello world" def class_method(self): #every class method must have a conventional self argument print("This method print Hello")
Behind the Code:
In the above example, we have uses class keyword to create a class, name My_class and inside the class block, it has two attributes class_atrribute_1 and class_attribute_2, and one method class_method. So the Question is how can we access the attributes and methods of class My_class, though we can use the class name itself to access its attributes and methods but it would make the class concept so limited that’s my we make class objects, the advantage of calling the class properties (methods and attributes) with objects that we can create as many as objects of a single class that increase the flexibility of using a class concept.
Create an Object:
We create an Object of a class to invoke the class properties. As we know that we can create more than one object of a single class that means we can create different models using a single blueprint.
Syntax of creating an object of a class:
object_name = Class_Name()
Example of creating an object:
Let’s create the object of the above class:
class My_class: class_attribute_1 = 40 class_attribute_2 = "Hello world" def class_method(self): print("This method print Hello") obj_1 = My_class() #create object of class My_class print("---------------Calling My_class Properties using obj_1------------------") print(obj_1.class_attribute_1) print(obj_1.class_attribute_2) obj_1.class_method()
---------------Calling My_class Properties using obj_1------------------ 40 Hello world This method print Hello
Behind the code:
In the above example you can see that when we are calling the class_method() using the class object obj_1 we do not pass any argument along the class_method, though it has a self as an argument inside a class, this is because when we call the class_method() using object obj_1 the object itself become the argument for that method, that’s why we write self because here the object itself becomes an argument.
As we know that the methods which have double underscore before and after their names are known as magical methods and __init__() is an inbuilt magical method which is also known as the constructor of a class.
A constructor is a method of a class which invoke automatically at the moment we create the object of that class.
Class constructor or __init__() method syntax:
class Class_name: def __init__(self): # __init__ block
class Constructor: def __init__(self): print("We did not call this method but it called eventually") def method(self): print("To invoke this method you need object") obj = Constructor() #Here we created the object of the class
We did not call this method but it called eventually
Delete class attributes and objects:
We can delete class attributes and unwanted objects using the del keyword. Once you have deleted the object and attribute, they would not be there for use so be careful when you del keyword.
class My_class: class_attribute_1 = 40 class_attribute_2 = "Hello world" obj_1 = My_class() print(obj_1.class_attribute_1) print("---------------deleting obj_1------------------") del obj_1.class_attribute_1 print(obj_1.class_attribute_1)
40 ---------------deleting obj_1------------------ Traceback (most recent call last): print(obj_1.class_attribute_1) NameError: name 'obj_1' is not defined