Why Python Needs the “pass” Statement

Original article was published by Christopher Tao on Artificial Intelligence on Medium

Why Python Needs “pass” Statement

When to use “pass”? Are there any alternatives?

For anyone like me switched to Python programming language from other languages, it is quite common to find that Python has some statements that are very unique. The “pass” statement is one of them, which you rarely can find such a thing in other programming languages. I have been using Java, C, C#, JavaScript, Objective-C, Swift, Scala and so on, none of them has the “pass” statement.

In fact, the “pass” statement is a very simple expression in Python. In this article, I’m going to introduce the several different aspects of this unique statement including its usage, the scenarios that need to use it, the benefit of using it, the reason that Python needs it and finally the potential alternatives.


Photo by amazed on Pixabay

So, what is the “pass” statement in Python?

Generally speaking, the “pass” statement is merely a null operation. Specifically, when it is executed, nothing happens.

Therefore, the “pass” statement is usually utilised as a placeholder syntactically. When there is no code needs to be executed, we can put the “pass” statement over there.


Photo by sentidocomun on Pixabay

Then, what are the scenarios that we may or need to use the “pass” statement? In fact, it is not limited to be used in any certain scenarios. However, of course, it doesn’t make sense to put it everywhere. So, here I have summarised five different typical scenarios.

1. In a loop

The “pass” statement can be used in a loop statement such as for-loop.

for i in range(10):

Or a while-loop.

while true:

So, we don’t need to write any logic in the loops because the “pass” statement acts as a placeholder that does nothing.

2. In an if-else condition

When we defined an if-else condition, we can decide the conditions first and leave the conditional statement later.

if a == 2:

3. In a function

It is also utilised in a function very commonly.

def func(param):

When we want to define a function, but want to implement it later, we can use the “pass” statement.

4. In a class

Sometimes we want to create a class but implement it later, such as follows.

class Cat(Animal):
class Dog(Animal):

We know that cats and dogs are animals, so we want to create these child classes and implement them later.

5. In a try-expect clause

This is another common use case of the “pass” statement.


In this case, we want to use the try-except statement but we do not actually care what the exception is. So, we can just use the “pass” statement.

Benefits of Using “pass”

Photo by AbsolutVision on Pixabay

We have been talked about a lot of things about the “pass” statement. However, why we need to use it? In other words, what are the benefits of using it?

First of all, although the “pass” statement does nothing, it still can be used as what we used to do in other programming languages — //TODO. One of the major difference between a comment and the “pass” statement is that the later can be coloured by most of the IDE tools because it is a keyword.

Also, unlike a piece of comments that will be completely ignored, the “pass” statement is valid code after all. It will be recognised by the interpreter. Therefore, it is forced to follow the indentation of Python syntax, as well as helping to construct a completed code snippet.

Why Python needs “pass”?

Photo by Bru-nO on Pixabay

Let’s try a function without using the “pass” statement.

No matter whether we put the #TODO comment or not, the code cannot be executed, because Python relies on indentation but not curly braces {}. The comment #TODO is indented, but it will be ignored by the interpreter. Therefore, we have to use the “pass” statement. It is a piece of valid code, which will not be ignored by the interpreter and become the body of the function.

Let’s go one step further. If we just need a piece of valid code, the “pass” statement might not be the only choice. For example, we can use a string or a number but without assigning them into any variables as follows.

This time we got compiling passed.

However, the string and number make no sense to be put in the function body, and it is very obvious that the “pass” statement is more neat and elegant.

Other Alternatives

Photo by qimono on Pixabay

There are indeed some other keywords can achieve similar outcomes as the “pass” statement, such as return, yield, continue and break.

Why we don’t use them?

Firstly, the “pass” statement will never change the order of code execution, as well as never cause any effects in its own domain. So, it is the safest option comparing to any other keywords if we use them for the same purpose.

That is,

  • return and yield will stop a function from execution
  • break will stop a loop
  • continue will stop the current loop

Apart from that, these keywords are not generalised. They can only be used in certain scenarios.

  • return and yield can only be used in a function
  • break and continue can only be used in a loop

Consequently, although some keywords can be used for the same purpose, the “pass” statement is recommended.


Photo by Engin_Akyurt on Pixabay

In this article, I have introduced one of the unique syntaxes in Python, which is the “pass” statement. It is a piece of valid code that stands for “null operation” which is usually utilised as a placeholder in a loop, a function, a class, etc. There are several benefits of using it such as the IDE will colour it as a keyword. Also, Python needs to provide such a keyword as it relies on indentation to define a domain of a function, etc. So, it is needed to use a piece of valid code as a placeholder, rather than the “TO DO” comment that is used in most of the other programming languages. Finally, although there are some other keywords such as “return” can be used to achieve the same thing, but it is not generalised enough and not as neat as the “pass” statement.