Object-Oriented Programming (OOP) is a significant programming paradigm in programming languages. In Python, everything is an object. For code reusability, OOP is unparalleled. In this context, a class is the fundamental structural unit.
Class
A class acts as a blueprint for an object. Just like a mold in a brick kiln that is used to create numerous bricks of the same shape, in programming, a class can be used to create numerous objects of the same structure. Let’s see how to create a class in Python.
# This is an example of a class in Python
class TestClass:
name="Python"
classObject=TestClass() # creating an object
print(classObject.name) # Accessing a class variable
Output:
Python
Here, a class named TestClass
is created. The structure of a class begins with the class
keyword followed by the class name and a colon, with the class elements written within the indented block. In this example, a single variable name
is used.
In line 5, an object of the class is created. Here, classObject
is an object of TestClass
.
In line 7, the name
variable is accessed using the object. In Python, the dot operator (.) is used to access class variables.
Instance of a Class
The previous program was quite basic and had no practical application. Let’s delve a bit deeper with an example:
# This is an example of a class in Python
class MathClass:
def __init__(self,x,y):
self.x=x
self.y=y
def Multiply_function(self):
z=self.x*self.y
return z
firstObject=MathClass(5,10) # creating an object
secondObject=MathClass(2,4) # creating an object
print(firstObject.Multiply_function()) # Accessing a class function
print(secondObject.Multiply_function()) # Accessing a class function
Output:
50
8
In line 2, a class named MathClass
is defined. In line 6, a function named Multiply_function
is declared which uses the self
parameter. The self
keyword is used to refer to the same class.
__init__ Function
Now the question is, what is the __init__
function and why is it used? The __init__
function is a default function in every class. It is primarily used to initialize an object. When an object is created, this function is called first and an instance is created, known as an instance of the class. It is also called a constructor.
In the above program, two objects are initialized in lines 10 and 11. Notice that two values are passed inside the class, and these values initialize the class.
In both cases, the same variables x
and y
are used, but the values for each object are different. Thus, the class acts as a mold, and different objects are created from it. To understand this better, consider the following example:
Output:
5
2
It is clear here that while the class variable (mold) is the same, the elements vary according to the object (product).
Modifying an Object Property
An object’s property can be modified in the same way, using the dot operator. For example:
# This is an example of a class in Python
class MathClass:
def __init__(self,x,y):
self.x=x
self.y=y
def Multiply_function(self):
z=self.x*self.y
return z
firstObject=MathClass(5,10) # creating an object
print(firstObject.Multiply_function()) # Accessing a class function
firstObject.x=15 #Modifying the class property
print(firstObject.Multiply_function()) # Accessing a class function after modifying the class property
Result:
50
150
In lines 12 and 16, the function is called normally, but due to the change in value at line 14, the result is different.
Deleting an Object Property
To delete an object property, the del
keyword is used. Here’s an example:
# This is an example of a class in Python
class MathClass:
def __init__(self,x,y):
self.x=x
self.y=y
def Multiply_function(self):
z=self.x*self.y
return z
firstObject=MathClass(5,10) # creating an object
print(firstObject.x) # Accessing a class property
del firstObject.x # Delete the class property
print(firstObject.x) # Accessing a class property after modifying the class property
Result:
5
Traceback (most recent call last):
File "main.py", line 24, in <module>
print(firstObject.x) # Accessing a class function after modifying the class property
AttributeError: 'MathClass' object has no attribute 'x'
From the result, we see that 5
is printed before deleting the property. After the deletion, trying to access the property results in an AttributeError
, indicating that the property no longer exists.
Deleting an Object
So far, we’ve seen how to delete properties. Now let’s see how to delete an entire object:
# This is an example of a class in Python
class MathClass:
def __init__(self,x,y):
self.x=x
self.y=y
def Multiply_function(self):
z=self.x*self.y
return z
firstObject=MathClass(5,10) # creating an object
print(firstObject.x) # Accessing a class function
del firstObject # Deleting the class
print(firstObject.x) # Accessing a class variable after deleting the class property
Result:
5
Traceback (most recent call last):
File "main.py", line 24, in <module>
print(firstObject.x) # Accessing a class variable after deleting the class property
NameError: name 'FirstObject' is not defined
The result shows 5
before deleting the object. After deletion, trying to access the object results in a NameError
, indicating that the object no longer exists.