Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

What does Python's pass statement do?


May 28, 2021 Article blog


Table of contents


With regard to the pass statement in Python, it seems simple (only 4 letters), and even beginners with no programming experience can quickly master its usage.

The introduction to the official document is simple, and the following three examples give us a quick idea of how to use it:

 What does Python's pass statement do?1

Simply put, pass is an empty operation, and when the interpreter executes on it, skip it without doing anything other than check that the syntax is legitimate.

The biggest difference between it and non-empty operations such as return, break, spare, and yield is that it does not change the order in which the program is executed. It's like a comment we write, and it doesn't have any effect on the scope except that it takes up a line of code.

However, if you have the basis of other languages, you may wonder: Why does Python have such a unique pass statement, while other languages do not?

What is the reason why Python is so designed?

Is it to solve the common problems that most programming languages face, or is it to create a new feature because it has its own new discoveries?

In other words: Why does Python have a pass statement, what problems (benefits) can it solve, and what problems (harm) would it cause without it?

Next, this article will expand the analysis from two dimensions.

1, to people: as a space placeholder

I see it as a concise way of commenting, which is to say, "Reserve a place here, go back and make up the specific code implementation."

For example, in a multi-layered if-elif-else structure, we can write the judgment criteria first, then pass in the corresponding block, and then slowly perfect it later.

For example, in the example given above, we can write the class/function name and its input, and then skip the (pass) body code, and then slowly fill in.

Pass is simple to write, and because it's a keyword, the IDE gives a conspicuous color distinction, so it's easier than we write notes.

Pass as a space placeholder, mainly can facilitate us to conceive local code structure, has a certain auxiliary reminder role.

However, as a way of commenting, it is too thin to be written as "todo: xxxx", which is also highlighted by the IDE in color and has a clearer meaning. Although simple to write, it also introduces a seemingly redundant keyword pass.

Therefore, from the point of view of space placeholders, pass is not a necessary design element in a programming language.

With it, we can express the semantics of "there's something here, but skip it for a while," but without it, we can replace it with comments.

2, on the machine: for the sake of syntax integrity

For the use of the previous article, the position of pass in the code is theoretically unlimited.

However, when we most often use pass, it is basically on the next line of the colon, and in the indented block of code at that layer, there is only one statement. (See the 3 examples above, for convenience, we only take empty functions as an example)

We can imagine what would happen if we didn't write it.

The answer is an indentation error: IndentationError: expected an indented block

# 将函数体的 pass 去除,会报错
def func():

func()

Because Python uses indentation to divide blocks of code (for reasons, see Why does Python use indentation to divide blocks of code?). ), and the colon identifies a new indented block of code to appear, so this example will report a lack of indented blocks of code.

If we replace it with the comments mentioned above, see what happens?

# 将函数体的 pass 换成注释
def func():
    # todo:此处有东西,以后补上
func()

Writing this way, it will also be reported incorrectly: IndentationError: expected an indented block

The reason is that a comment is not a valid syntax content, it is ignored by the Python interpreter (ignore) and is not "valid syntax content, but skipped" as the pass statement.

That is, the indented block of code must contain syntax content, and the following examples are valid:

def func():
    """这是一个字符串"""

def func2():
    123456

Python must include a function body when defining a function, that is, both declarations and definitions, not as some languages can use only declared semantics, that is, written as void test();

However, because Python does not use curly braces, it cannot define empty functions directly, as some languages do, that is, written as void test().

Taken together with the above analysis, Python must have a legitimate body of functions when defining empty functions, so it has designed a pass statement that represents an empty operation. It is intended to complement the integrity of the syntax, along with colons, equivalent to a pair of empty braces in other languages.

In terms of the dimension of syntax integrity, it is a necessary design element, and if not, it must be replaced by similar empty statements or special symbols.

On the human side, pass can mean "skip temporarily" and, as a temporary placeholder, will eventually be replaced by the actual code implementation, or on the machine side, it can mean "skip directly" just to complement the syntax logic and not be replaced by other code.

Other languages do not have a specific statement or symbol to represent this placeholder (i.e. semantically deficient), but they do not need to bother designing a keyword specifically to complement syntax integrity (i.e. complete syntax).

Back to the question at the beginning of this article: Why does Python have a pass statement, what problems (benefits) can it solve, and what problems (harm) would it cause without it?

Python uses pass statements to support blocks of code (empty functions, empty classes, empty loop control blocks, and so on) that support purely empty operations, and with it, it can also express the semantics of a placeholder.

The former is for machines and must have the effect of empty curly braces in other languages; the latter is human, non-essential, and can be expressed with comments, but because Python designed this statement, this usage is sometimes convenient.

Reprinted from: Rookie Python

That's what Python's pass statement does for you? the whole content.