How to create an empty class in Python?


A class in Python user-defined prototype for an object that defines a set of attributes that characterize any object of the class. The attributes are data members (class variables and instance variables) and methods, accessed via dot notation.

We can easily create an empty class in Python using the pass statement. This statement in Python do nothing. Let us see an example 

Create an empty class

Here, our class name is Amit −

class Amit:
   pass

Create an empty class with objects

Example

We can also create objects of an empty class and use it in our program 

class Amit: pass # Creating objects ob1 = Amit() ob2 = Amit() # Displaying print(ob1) print(ob2)

Outpu

<__main__.Amit object at 0x7f06660cba90>
<__main__.Amit object at 0x7f06660cb550>

Create an empty class and set attributes for different objects

Example

In this example, we will create an empty class using the pass, but attributes will also be set for objects −

class Student: pass # Creating objects st1 = Student() st1.name = 'Henry' st1.age = 17 st1.marks = 90 st2 = Student() st2.name = 'Clark' st2.age = 16 st2.marks = 77 st2.phone = '120-6756-79' print('Student 1 = ', st1.name, st1.age, st1.marks) print('Student 2 = ', st2.name, st2.age, st2.marks, st2.phone)

Output

Student 1 =  Henry 17 90
Student 2 =  Clark 16 77 120-6756-79

Using the pass statement, we can also create empty functions and loops. Let’s see 

Empty Function

Use the pass statement to write an empty function in Python 

# Empty function in Python def demo(): pass

Above, we have created an empty function demo().

Empty if-else Statement

The pass statement can be used in an empty if-else statements 

a = True if (a == True) : pass else : print("False")

Above, we have created an empty if-else statement.

Empty while Loop

The pass statement can also be used in an empty while loop 

cond = True while(cond == True): pass

Updated on: 15-Sep-2022

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements