# Pass

The pass statement, simply speaking, is a null statement. Furthermore, the general use of this statement takes place as a placeholder, that is when the user has no clue what code to write. Therefore, the user would have to simply place this statement at that line.

### Understanding Pass Statement

The pass statement in python, a programming language, is characterized as a null operation. This means that nothing happens when its execution takes place. Furthermore, the pass is also useful in places where one’s code would eventually go, but its writing has not been done yet. The example of this can be in stubs.

Sometimes the use of pass takes place when the user doesn’t want any execution of a code. So, the user simply places pass there as empty code is not permissible in if statements, class definitions, loops, or function definitions. Therefore, the user would be able to avoid this error by making use of the pass statement.

Syntax:

Example

Live Demo

                    
#!/usr/bin/python

for letter in 'Python':

if letter == 'h':

pass

print 'This is pass block'

print 'Current Letter :', letter

So, print "Good bye!"

When the execution of the above code takes place, it produces the following result −

Current Letter : P

Current Letter : y

Moreover, Current Letter : t

This is pass block

Current Letter : h

Current Letter : o

Finally, Current Letter : n

Good bye!



### Future Code

There are many cases where there is a requirement for a structure of the code to use a block. In such a case, one may eventually have to write code there. Moreover, in such cases, it may be hard to get out of the flow of working on something specific and begin focusing on a dependency.

In these cases, a pass statement turns out to be very useful. This is because to do the minimal amount of work will have to be for the dependency so one can appropriately focus on the main work.

As a concrete example, imagine writing a function that facilitates the processing of a string. Afterwards, the writing of the result takes place by both writes to a file and then it is returned:

                    
def get_and_save_middle(data, fname):

middle = data[len(data)//3:2*len(data)//3]

save_to_file(middle, fname)



### return middle

This function saves and facilitates the returning of the middle third of a string. There is no need to finish implementing save_to_file() before the testing of the output for an off-by-one-error. However, if save_to_file() doesn’t exist in any form, then one would get an error.

It is also possible to comment out the call to save_to_file(), but then one would have to uncomment the call after the confirmation that get_and_save_middle() works well. Instead, one may quickly implement save_to_file() with a pass statement:

def save_to_file(data, fname):

pass # TODO: fill this later

This function allows one to test get_and_save_middle() without errors.

### Commented Out Code

When you comment out code, the invalidation of the syntax is possible by removing all code in a block. If you have an if … else condition, then the useful thing to do is to comment out one of the branches:

                    
def process(context, input_value):

if input_value is not None:

expensive_computation(context, input_value)

else:

logging.info("skipping expensive: %s", input_value)



In this example, expensive_computation() runs code that takes a long time, like the multiplication of the big arrays of numbers. While debugging is taking place, you may have to temporarily comment out the expensive_computation() call.

Consider a case where one may want to run this code against some problematic data. Furthermore, this way one would be able to find out why there are so many values that aren’t None by checking out the logs for the relevant description. Here, one would be able to speed up the testing process by skipping the expensive computation for the valid values.

However, this isn’t valid code:

                    
def process(context, input_value):

if input_value is not None:

# Temporarily commented out the expensive computation

# expensive_computation(context, input_value)

else:

logging.info("skipping expensive: %s", input_value)



In this example, the if branch is devoid of any statements in it. Furthermore, the stripping of the comments takes place early in the parsing process, before inspection of the indentation happens to find out where blocks begin and end.

In this case, adding a pass statement would certainly make the code valid:

                    
def process(context, input_value):

if input_value is not None:

# Temporarily commented out the expensive computation

# expensive_computation(context, input_value)

# Added pass to make code valid

pass

else:

logging.info("skipping expensive: %s", input_value)



Now it’s possible to run the code and generate the logs with useful information. Most noteworthy, all of this code can happen while skipping the expensive computation.

## FAQs For Pass

Question 1: Differentiate between a comment and a pass statement in python?

Answer 1: The difference between a comment and a pass statement in Python can be on the basis of ignoring. So, an interpreter would ignore a comment entirely but would not ignore a pass.

Question 2: Explain what is pass in python and how is it useful?

Answer 2: In Python, the pass keyword happens to be an entire statement in itself. Furthermore, the pass statement is characterized as a null operation, meaning that nothing happens when its execution takes place. Also, the python statement is surprisingly useful for a statement that does nothing.

Sometimes the pass is useful in the final code whose running takes place in production. More often, the pass is useful as scaffolding during the development of a code.

Share with friends

## Customize your course in 30 seconds

##### Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
Ashhar Firdausi
IIT Roorkee
Biology
Dr. Nazma Shaik
VTU
Chemistry
Gaurav Tiwari
APJAKTU
Physics
Get Started

Subscribe
Notify of