Home  >  Article  >  Backend Development  >  How to create an empty class in Python?

How to create an empty class in Python?

PHPz
PHPzforward
2023-08-31 12:08:521534browse

How to create an empty class in Python?

A class in Python is a user-defined object prototype that is used to define the properties of any object that characterizes a set of classes. These properties include data members (class variables and instance variables) and methods, which can be accessed through dot notation.

We can easily create an empty class in Python using the pass statement. In Python, this statement does nothing. Let’s see an example

Create an empty class

Here, our class name is Amit −

class Amit:
   pass

Create an empty class and instantiate the object

Example

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

class Amit:
   pass

# Creating objects
ob1 = Amit()
ob2 = Amit()

# Displaying
print(ob1)
print(ob2)

Output

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

Create an empty class and set properties for different objects

Example

In this example, we will create an empty class using pass, but also set the properties

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 created an empty function demo().

Empty if-else statement

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

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

Above, we 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

The above is the detailed content of How to create an empty class in Python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete