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

How to create an empty class in Python?

PHPz
Release: 2023-08-31 12:08:52
forward
1583 people have browsed it

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
Copy after login

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)
Copy after login

Output

<__main__.Amit object at 0x7f06660cba90>
<__main__.Amit object at 0x7f06660cb550>
Copy after login

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)
Copy after login

Output

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

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
Copy after login

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")
Copy after login

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
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template