Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to create an empty class in Python?
A class in Python is a 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 does nothing and acts as a placeholder ?
Basic Empty Class Syntax
Here's how to create a simple empty class ?
class Student:
pass
print("Empty class created successfully!")
Empty class created successfully!
Creating Objects from Empty Class
We can create objects of an empty class and use them in our program ?
class Student:
pass
# Creating objects
obj1 = Student()
obj2 = Student()
# Displaying object references
print("Object 1:", obj1)
print("Object 2:", obj2)
print("Are they the same object?", obj1 is obj2)
Object 1: <__main__.Student object at 0x7f06660cba90> Object 2: <__main__.Student object at 0x7f06660cb550> Are they the same object? False
Adding Dynamic Attributes
Empty classes are flexible ? you can add attributes to objects dynamically after creation ?
class Student:
pass
# Creating objects and adding attributes
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)
Student 1: Henry 17 90 Student 2: Clark 16 77 120-6756-79
Other Uses of pass Statement
Empty Function
The pass statement can create empty functions for placeholder purposes ?
def calculate_grade():
pass
def send_notification():
pass
print("Empty functions defined successfully!")
Empty functions defined successfully!
Empty Control Structures
Use pass in empty conditional statements and loops ?
# Empty if-else
status = True
if status:
pass # TODO: Add implementation later
else:
print("Status is False")
# Empty for loop
items = [1, 2, 3]
for item in items:
pass # Placeholder for future logic
print("Empty control structures created!")
Empty control structures created!
When to Use Empty Classes
Empty classes are useful for:
- Prototyping: Creating class structure before implementing methods
- Data containers: Simple objects to hold related data
- Placeholders: Temporary classes during development
- Namespace objects: Grouping related functions or constants
Conclusion
Empty classes in Python use the pass statement as a placeholder. They allow dynamic attribute assignment and serve as useful prototypes during development. The pass statement also works for empty functions and control structures.
