Classes and objects are the key elements that you need to work with if you want to become a Python developer. In this tutorial, we will discuss Python class and objects that are among the most important topics of the Python programming language. Also, you will learn you can create a class and its objects. So, let's get started.
What are Python Class and Objects?
Python is an object-oriented programming (OOP) language. In object-oriented programming, we use class and objects to make a program. To understand OOP, let’s compare it with procedural programming. Though Python supports both procedural as well as object-oriented programming, it is better known for leveraging OOP concepts. In procedural programming , we mostly divide our code into different functions to make it modular. Whereas in OOP, 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) that comes into 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.
How to Create a Class in Python?
To create a user-defined function, we use the def keyword. To define a class, we use the 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 that contains additional information about the class. Class syntax class Class_Name: ''' class docstring''' # Class block Pass Example
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 used the class keyword to create a class named My_class and inside the class block, it has two attributes - namely class_atrribute_1 and class_attribute_2 , and one method named class_method . So the question that arises here is that 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, it would make the class concept limited. So, what we need to do is to call the class properties. The key advantage of calling the class properties (methods and attributes) is that we can create as many objects as we want of a single class, and this will increase the flexibility of using the class concept.
How to Create an Object?
We create an object of a class to invoke the class properties. We can create more than one object of a single class, which means that we can create different models using a single blueprint. Syntax for creating an object of a class object_name = Class_Name() Example 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 an argument inside a class. This is because when we call the class_method() using object obj_1 , the object itself becomes the argument for that method. Also, we write self because here the object itself becomes an argument.
Methods that have double underscore before and after their names are known as magical methods, and __init__() is an inbuilt magical method that is also known as the constructor of a class. A constructor is a method of a class that invokes 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 use the del keyword. Example
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)
#Output 40 ---------------deleting obj_1------------------ Traceback (most recent call last): print(obj_1.class_attribute_1) NameError : name 'obj_1' is not defined