
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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
- Related Articles
- How to create an empty list in Python?
- How to create an empty dictionary in Python?
- How to create an empty tuple in Python?
- How to create an empty file using Python?
- How to create and set an Empty Border from BorderFactory class in Java?
- How to create an empty VIEW in MySQL?
- How to create an empty matrix in R?
- How to create an empty array in Kotlin?
- How to create an empty data frame in R?
- Create empty border with BorderFactory class in Java?
- How to create class objects in Python?
- How to create an empty plot using ggplot2 in R?
- How to write an empty function in Python?
- How to create an immutable class in Java?
- How do you create an empty list in Java?
