Open In App

How to create an empty class in Python?

Last Updated : 29 Dec, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

A class is a user-defined blueprint or prototype from which objects are created. Class can be considered as a user-defined data type. Generally, a class contains data members known as attributes of the class and member functions that are used to modify the attributes of the class. But have you ever wondered how to define an empty class i.e a class without members and member functions?

In Python, if we write something like the following, it would raise a SyntaxError.




# Incorrect empty class in 
# Python
  
class Geeks:


Output:

  File "gfg.py", line 5

                ^
SyntaxError: unexpected EOF while parsing

In Python, to write an empty class pass statement is used. pass is a special statement in Python that does nothing. It only works as a dummy statement. However, objects of an empty class can also be created.

Example:




# Python program to demonstrate
# empty class
  
class Geeks:
    pass
  
# Driver's code
obj = Geeks()
  
print(obj)


Output:

<__main__.Geeks object at 0x02B4A340>

Python also allows us to set the attributes of an object of an empty class. We can also set different attributes for different objects. See the following example for better understanding.




# Python program to demonstrate
# empty class
  
  
class Employee:
    pass
  
  
# Driver's code
# Object 1 details
obj1 = Employee()
obj1.name = 'Nikhil'
obj1.office = 'GeeksforGeeks'
  
# Object 2 details
obj2 = Employee()
obj2.name = 'Abhinav'
obj2.office = 'GeeksforGeeks'
obj2.phone = 1234567889
  
  
# Printing details
print("obj1 Details:")
print("Name:", obj1.name)
print("Office:", obj1.office)
print()
  
print("obj2 Details:")
print("Name:", obj2.name)
print("Office:", obj2.office)
print("Phone:", obj2.phone)
  
  
# Uncommenting this print("Phone:", obj1.phone)
# will raise an AttributeError


Output:

obj1 Details:
Name: Nikhil
Office: GeeksforGeeks

obj2 Details:
Name: Abhinav
Office: GeeksforGeeks
Phone: 1234567889
Traceback (most recent call last):
  File "gfg.py", line 34, in 
    print("Phone:", obj1.phone)
AttributeError: 'Employee' object has no attribute 'phone'


Previous Article
Next Article

Similar Reads

Python | Create an empty text file with current date as its name
In this article, we will learn how to create a text file names as the current date in it. For this, we can use now() method of datetime module. The datetime module supplies classes for manipulating dates and times in both simple and complex ways. While date and time arithmetic is supported, the focus of the implementation is on efficient attribute
1 min read
How to Create an Empty Figure with Matplotlib in Python?
Creating a figure explicitly is an object-oriented style of interfacing with matplotlib. The figure is a basic building block of creating a plot as Matplotlib graphs our data on figures. This figure keeps track of all other components such as child axes, legends, title, axis, etc. Steps to create an empty figure : First, we import the matplotlib li
2 min read
How to create an empty matrix with NumPy in Python ?
The term empty matrix has no rows and no columns. A matrix that contains missing values has at least one row and column, as does a matrix that contains zeros. Numerical Python (NumPy) provides an abundance of useful features and functions for operations on numeric arrays and matrices in Python. If you want to create an empty matrix with the help of
3 min read
Create an empty file using Python
File handling is a very important concept for any programmer. It can be used for creating, deleting, and moving files, or to store application data, user configurations, videos, images, etc. Python too supports file handling and allows users to handle files i.e., to read and write files, along with many other file handling options, to operate on fi
3 min read
How to create an empty PyGame window?
Pygame window is a simple window like any other window, in which we display our game screen. It is the first task we do so that we can display our output onto something. Our main goal here is to create a window and keep it running unless the user wants to quit. To perform these tasks first we need to install pygame package and import some pre-defin
2 min read
Wand - Create empty image with background
In Python we can create solid backgrounds using Wand. We can use these backgrounds for further use in image. We can use these backgrounds in a backgroundless image to make it more attractive. This can be done by simply using Image() function and setting width, height and background parameter. Syntax : C/C++ Code with Image(width=&lt;i&gt;image_widt
1 min read
wxPython - Create empty window
wxPython is one of the most famous library in python for building GUI applications. In this first article of wxPython we will build an empty window using wxPython Library. Steps to create empty window : 1. Import wx in your code 2. Create wx.App object 3. Create object for wx.Frame 4. Show frame using Show() function C/C++ Code # Import wx module i
1 min read
Create empty dataframe in Pandas
The Pandas Dataframe is a structure that has data in the 2D format and labels with it. DataFrames are widely used in data science, machine learning, and other such places. DataFrames are the same as SQL tables or Excel sheets but these are faster in use.Empty DataFrame could be created with the help of pandas.DataFrame() as shown in below example:
1 min read
How to create an empty and a full NumPy array?
NumPy is a crucial library for performing numerical computations in Python. It offers robust array objects that enable efficient manipulation and operations on extensive datasets. Creating NumPy arrays is an essential process in scientific computing and data analysis. In this article, we will explore how to create both empty and full NumPy arrays,
3 min read
How to create an empty PySpark DataFrame ?
In this article, we are going to see how to create an empty PySpark dataframe. Empty Pysaprk dataframe is a dataframe containing no data and may or may not specify the schema of the dataframe. Creating an empty RDD without schema We'll first create an empty RDD by specifying an empty schema. emptyRDD() method creates an RDD without any data.createD
3 min read