How to Create Empty Class in Python?

  • Python
  • 1 min read

In Python, you can create an empty class by using the pass command after the definition of the class object, because one line of code is compulsory for creating a class. Pass command in Python is a null statement; it does nothing when executes. The following is an example of an empty class object in Python.

Create an Empty Class in Python Example

In the below Python program, we will create an empty class customer. However, we can still define the objects outside of the customer class and would be able to use in our Python program.

class customer:
    pass

customer1 = customer()

customer1.first_name = 'John'
customer1.last_name = 'Greenberg'

customer2 = customer()

customer2.first_name = 'Nancy'
customer2.last_name = 'Lorrentz'
#customer 2 is also having a phone number
customer2.phone = '893-039-239'

print('Customer 1:', customer1.first_name, customer1.last_name)
print('Customer 2:', customer2.first_name, customer2.last_name, 'Phone No.', customer2.phone)

Output

Customer 1: John Greenberg
Customer 2: Nancy Lorrentz Phone No. 893-039-239

See also:

This Post Has 3 Comments

  1. Nikita

    What's the need of creating empty class btw?

    1. Vinish Kapoor

      There are several advantages of empty class in Python.

      The one you can see in the above example, that the customer class is not static class, it is a kind of a dynamic class now. You can add any attributes to it.

  2. Netster

    I’m learning Python right this moment. Thank you for the post

Comments are closed.