Python Classes and Objects
Python is an Object-Oriented Programming (OOP) language, and every element (attributes, functions, etc.) in the python program is an object of a class.
For example, if you use any element such as string, number, list, etc., in a python program, those are the objects of built-in classes.
print(type(id))
name = "Suresh"
print(type(name))
marks = [1, 2, 3]
print(type(marks))
The above example will return the result as shown below.
<class 'str'>
<class 'list'>
In the programming world, class is a collection of various data members such as properties, attributes, functions, etc., and the object is an instance of a class to access the defined data members from the class. We can also say the object is the blueprint of a class.
Generally, the OOP concepts are useful to create reusable code by reducing code duplication. Now, we will learn the class, the object, and how to use classes and objects in python with examples.
Class in Python
In python, the class is a combination of data members such as constructors, properties, attributes, etc., and member functions.
Create a Class
To create a class in python, you need to use the class keyword as shown below.
'''Class Creation'''
properties
functions
....
....
Python Class Example
Following is the example of creating a class in python.
id = 10
name = "Suresh"
def userdetails(self):
'''Function to get user details'''
print("Id: {}".format(self.id))
print("Name: {}".format(self.name))
If you observe the above python example, we created a class(user) using a class keyword with required variables and functions.
The self keyword in python is useful to refer to the current instance of the class. To access the class level variables, we used the self keyword in the userdetails() function.
Object in Python
As discussed, the object is an instance of a class to access the defined data members. The object will act as a blueprint of a class.
We can create the object of the above user class as shown below.
After creating the object for the user class, you can access the class properties as shown below.
print(u1.id)
print(u1.name)
u1.userdetails()
Python Objects Example
Following is the example of creating the object and accessing the class properties in python.
id = 10
name = "Suresh"
def userdetails(self):
'''Function to get user details'''
print("Id: {}".format(self.id))
print("Name: {}".format(self.name))
u1 = user()
u1.userdetails()
The above example will return the result as shown below.
Name: Suresh
Constructor in Python
In python, the constructor is a method that will invoke automatically whenever an instance of a class is created.
To create the constructor in a class, you need to use __init()__ method with self parameter as shown below.
#Constructor method
def __init__(self):
print("Invoked Constructor")
As discussed, the self keyword will refer to the current instance of a class. To access the class level variables, we used the self keyword in the constructor method (__init__).
Whenever an instance of a user class is created, the constructor method (__init__) will execute automatically.
Following is the example of creating a class with constructor in python.
id = 10
name = "Suresh"
def __init__(self, uid, uname):
print("Constructor Invoked")
self.id = uid
self.name = uname
def userdetails(self):
'''Function to get user details'''
print("EmpId: {}".format(self.id))
print("Name: {}".format(self.name))
u1 = user(2, "Rohini")
u1.userdetails()
The above example will return the result as shown below.
EmpId: 2
Name: Rohini
Create Empty Class
In python, creating a class with no content is not allowed. To create an empty class in python, you need to use the pass statement in a class definition.
Following is the example of creating an empty class in python using the pass statement.
pass
Modify Object Attributes
If required, you can modify the object attributes as shown below.
id = 10
name = "Suresh"
def __init__(self, uid, uname):
self.id = uid
self.name = uname
def userdetails(self):
print("Id: {}, Name: {}".format(self.id, self.name))
u1 = user(2, "Rohini")
u1.id = 30
u1.name = "Trishi"
u1.userdetails()
The above example will return the result as shown below.
Delete Attributes and Objects
By using the del keyword, you can delete the object attributes as shown below.
def __init__(self, id, name):
self.id = id
self.name = name
u1 = user(2, "Rohini")
del u1.id
print(u1.id)
When you execute the above program, you will get the result as shown below.
File "D:\pythonclasses.py", line 9, in
print(u1.id)
AttributeError: 'user' object has no attribute 'id'
You can also delete the complete object using the del keyword as shown below.
def __init__(self, id, name):
self.id = id
self.name = name
u1 = user(2, "Rohini")
del u1
This is how you can create the classes and objects in python to access the defined properties based on your requirements.
