In the previous chapter, we had discussed OOP concepts. In this tutorial, we will learn how to define a class and create an object in Python. Here, we will also discuss constructor in Python.
Define a Class in Python
Let’s begin with how to define a class in Python, to create a class in Python ‘class’ keyword is used. The syntax for creating a class.
class ClassName:
#statements
#statements
Now, we will look at an example to create a class Product, with an attribute price.
#creating a class
class Product:
price=2400
Attribute and Function in the Class
We can define attribute(variable) and behaviour(function) both in the class.
#creating a class, Product
class Product:
price=2400
#define function in class
def show(self):
print("Hello")
Create an Object
We need to create an object to access a member of the class. To create an object(instance), we will use the class name.
#creating object
p1=Product()
#delete an object
del p1
Access Member of the Class
In Python, we can access a member of the class by object reference.
#creating a class, Product
class Product:
price=2400
#define function in class
def show(self):
print("Hello")
#creating object
p1=Product()
#access variable
p1.price=1800
print(p1.price)
#access function
p1.show()
Constructor in Python
A constructor is a special type of function used to initialize the member of a class. In Python, __init__() function that begins with a double underscore(_ _), it’s used as a constructor.
- The constructor is called itself every time, whenever an object is created.
- It is used to initialize the variables of the class.
- Parameterised and none parameterised, we can use the constructor in two ways.
constructor without parameter
class Product:
def __init__(self):
self.pid=1001
self.pname="Laptop"
def show(self):
print("Product ID of",self.pname,"is",self.pid)
p1=Product()
p1.show()
like a method you need not call a constructor, it is called itself every time whenever we create an object.
constructor with parameter
We can create more than one object of a class. We must pass the parameter at the time of object creation if the constructor is parameterised.
#parameterised constructor example
class Product:
def __init__(self,pid,pname):
self.pid=pid
self.pname=pname
def show(self):
print("Product ID of",self.pname,"is",self.pid)
p1=Product(1001,"Laptop")
p2=Product(1002,"Tablet")
p1.show()
p2.show()
self is not a keyword in Python
We have seen self as a keyword in method definition and variable initialisation, it represents the instance of the class. By using self, we can access function and variables anywhere in the class. We can use any word instead of self, but it should be the first parameter of the function. Above example can also write replacing self with other words.
#replace self with any word
class Product:
def __init__(me,pid,pname):
me.pid=pid
me.pname=pname
def show(me):
print("Product ID of",me.pname,"is",me.pid)
p1=Product(1001,"Laptop")
p2=Product(1002,"Tablet")
p1.show()
p2.show()
Mini Quiz
Mini Project
Mini Project: Book Information System
Question:
Write a Python program to create a class that stores and displays book information.
Perform the following tasks:
- Create a class named
Book. - Create a parameterized constructor to accept the book title and price.
- Create a method
show()to display the book details. - Create two objects and display their information.
View Answer Code
class Book:
def __init__(self, title, price):
self.title = title
self.price = price
def show(self):
print("Book:", self.title)
print("Price:", self.price)
b1 = Book("Python Programming", 499)
b2 = Book("Data Structures", 599)
b1.show()
print()
b2.show()
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
Shubhranshu Shekhar is a coding instructor, mentor, and founder of VSIT Delhi with 20+ years of teaching experience (since 2004). He has guided many students who are now working in multinational companies and specializes in Full Stack Development, Python, Digital Marketing, and Data Analytics.