Skip to content
Home » The pass Statement in Python

The pass Statement in Python

Spread the love

Today we’ll be talking about the pass statement.

The pass statement can be used when a statement is required syntactically but we don’t want to deliver it. The result of the pass statement is null – nothing happens. We often use it in code stubs that are to be filled in later on.

Here’s an example. In the for loop there is supposed to be some loop body, i.e. some code executed inside the loop. Let’s see what happens if we don’t deliver any code:

 >>> for i in range(5):
 ... 
   File "<stdin>", line 2
     
     ^
 SyntaxError: unexpected EOF while parsing
 >>>  

We’re getting an error telling us that something that was expected was not found. Suppose we want to use this for loop, but we are going to think about its implementation only later. So, in order to get rid of the error for the time being we can use pass:

 >>> for i in range(5):
 ...     pass
 ... 
 >>>  

This code doesn’t do anything, but at least it doesn’t raise an error. You can also use the pass statement in function and class definitions.

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.


Spread the love
Tags:

Leave a Reply