Classes and Objects
As an object-oriented programming language, almost everything in Python is an object, with its attributes and methods.
A class is a “conceptual thing” and can be considered as a blueprint of an object. While, the object is the instance of a class.
The process of creating an object is referred to as instantiation. Basically the class comes into existence when it is instantiated.
Similar to the concept of defining functions with the keyword def, we can define a class using the keyword class.
Here, we create a class named Student with attributes like roll, name and age. After creating an object of the class, we will be able to access its attributes.
The following example shows this:
# Defining a class
class Student:
# Class Attributes
roll=101
name="Samar Das"
age=20
# Object creation
s = Student()
# Accessing the attributes
print("Roll:", s.roll)
print("Name:", s.name)
print("Age:", s.age)
Output:
Roll: 101
Name: Samar Das
Age: 20
Python Constructors:
The example shown above utilizes classes and objects in their simplest form, and are not really useful in building real-life applications. So, to develop such types of applications, we need to use the __init__() function in a class definition that begins with double underscore (__) . This special function is a constructor that gets called automatically whenever a new object of that class is instantiated.
In Python, a constructor is a special type of method (i.e. function) which is used to initialize the attributes of the class. Constructor definition is executed when we create the object of any class.
We may pass any number of parameters at the time of object creation, depending upon the definition of __init__(). Every class must have a constructor, even if it simply relies on the default constructor.
We can also use the self parameter as a reference to the current instance of the class, and is used to access attributes that belong to the class. Remember that self must always be first parameter in the __init__() function provided if it is present.
See the modified code below:
# Defining a class
class Student:
# Constructor
def __init__(self, roll, name, age):
self.roll = roll
self.name = name
self.age = age
# Object creation
s = Student(101, "Samar Das", 20)
# Accessing the attributes
print("Roll:", s.roll)
print("Name:", s.name)
print("Age:", s.age)
Output:
Roll: 101
Name: Samar Das
Age: 20
Object Methods
Python allows objects to contain methods inside the class definition. The methods in objects are functions that belong to those objects.
Let us create a method in the modified Student class. We may also use any name as a reference to the current instance of the class instead of having self. See the code below:
# Defining a class
class Student:
# Constructor
def __init__(self, roll, name, age):
self.roll = roll
self.name = name
self.age = age
# Method
def display(t):
print("Roll: ", t.roll)
print("Name: ", t.name)
print("Age: ", t.age)
print('\n')
# Object creation
s1 = Student(101, "Samar Das", 20)
s2 = Student(102, "Tina Sharma", 19)
# Accessing the object method
s1.display()
s2.display()
Output:
Roll: 101
Name: Samar Das
Age: 20
Roll: 102
Name: Tina Sharma
Age: 19
Delete attributes of object or object itself
In Python, we can delete attributes of object or object itself using the del keyword. Obviously, trying to access them will cause errors. See the example below:
# Defining a class
class Student:
# Constructor
def __init__(self, roll, name, age):
self.roll = roll
self.name = name
self.age = age
# Object creation
s = Student(101, "Samar Das", 20)
# Deleting an attribute
del s.roll
print("Roll:", s.roll) # AttributeError: 'Student' object has no attribute 'roll'
# Deleting the object
del s
# print("Age:", s.age) # undefined name 's'