paint-brush
Why Does Python Has a "Pass" Statementby@pawangeek
504 reads
504 reads

Why Does Python Has a "Pass" Statement

by Pawan JainJanuary 13th, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Python's 'pass' statement is a null operation that skips without doing anything except checking whether the syntax is legal. Python uses indentation to divide code blocks, and the colon marks the appearance of new indented code blocks. When Python defines an empty function, when an empty it must have a legal body, so the statement representing the operation is designed the empty. Python does not use curly braces because it cannot directly define an empty. function like some languages, that is defined as a function that is legal contains a legal. body. Python is designed to solve common problems that most programming languages ​​have to face.
featured image - Why Does Python Has a "Pass" Statement
Pawan Jain HackerNoon profile picture

Regarding the pass statements in Python, it seems to be very simple, and even beginners without any prior programming experience can quickly grasp its usage. If you’re a beginner in Python and I suggest you read control statements in Python first, to know about the pass, break and continue statements.


The introduction of the pass statement in the official document is very straightforward, and the following three examples can let us quickly understand how to use it:

Source: Python docs


To put it simply, pass is a null operation. And when the interpreter reaches it, it skips without doing anything except checking whether the syntax is legal.


  • Compared with non-null operations such as return, break, continue, and yield, the biggest difference is that it does not change the execution order of the program.
  • It is just like the comment we wrote, except that it occupies a line of code. It will not have any impact on the scope it is in.


However, suppose you know other languages like CPP, java, etc. In that case, you may be curious that Why does Python have such a unique pass statement, but other languages ​​don't?


Why Python is Designed This Way

Is it to solve the common problems that most programming languages ​​have to face, or because it has its own new discoveries, it creates a new feature.


To People: As a Space Placeholder

I think of it as a concise way of commenting, which is equivalent to saying, "First reserve a place, and then come back to add specific code implementation".


  • For example, in a multi-layer if-elif-else structure, we can write the judgment condition first, then write pass in the corresponding block, and gradually improve it later.
  • Another example is that we can first write the class/function name and its input parameters, skip the main code, and fill it in slowly.


To summarize Pass is simple to write, and because it is a keyword, the IDE will give a conspicuous colour distinction, so it is more convenient than writing comments.


As a space placeholder, pass is mainly convenient for us to conceive a partial code structure with a particular auxiliary reminder function. However, as a way of commenting, it seems not as good as writing "# todo", which will also be highlighted by the IDE in colour, and the meaning is more explicit. Although it's simple to write, it also introduces a seemingly redundant keyword pass.


Therefore, from the perspective of space placeholders, pass is not an essential design element in any programming language. With it, we can express the semantics of "there should be something here, but skip it for now", but if it does not have it, we can use the comment content instead.


To the Machine: For Grammatical Completeness

For the usage of pass as a comment, is theoretically limited.


However, when we use pass most often, it is basically on the line below the colon, and there is only this statement in the indented code block at this level. (See the 3 examples above, for convenience, we only take the empty function as an example)

We can imagine what will happen if we don't write it?

The answer is an indentation error will be reported: IndentationError: expected an indented block


# If you Remove the pass of the function body, an error will be reported

def func():

func()


Because Python uses indentation to divide code blocks, and the colon marks the appearance of new indented code blocks, this example will report the lack of indented code blocks .


If we use the comment mentioned above instead, what will happen? Write like this, it will also report an error: IndentationError: expected an indented block


# By adding comment

def func():
  # Code block will add something later
func()


The reason is that the comment is not a valid grammatical content. It will be ignored by the Python interpreter (bypass), unlike the pass statement that implies "valid grammatical content, but skips". In other words, the indented code block must contain grammatically meaningful content.


The following examples are all valid: When Python defines a function, it must include the function body, that is, it contains both the declaration and the definition.

def func():
  obj_a = 'Hello'

def func2():
  pass


It cannot be written like some languages that use the declaration semantics void test();. However, because Python does not support curly braces, it cannot directly define an empty function like other languages, that is, void test(){}.


Based on the above analysis, when python defines an empty function, it must have a legal function body, so the pass statement representing that an empty operation is designed.


It is to supplement the completeness of the grammar, together with the colon, which is equivalent to a pair of empty curly braces in other languages.


From the perspective of grammatical completeness, it is a necessary design element. If not, it must be replaced with similar empty sentences or special symbols. For humans, pass can mean "temporarily skip" as a temporary placeholder. It will eventually be replaced by the actual code implementation; for machines, it can mean "skip directly", to complement The grammatical logic will not be replaced by other codes.


Other languages ​​do not have a special kind of sentence or symbol to represent this kind of placeholder (that is, the semantics are lacking), but they do not need to bother to design a keyword to complete the grammatical completeness (that is, grammatical completeness).


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


Python uses the pass statement to support no-op code blocks (empty functions, empty classes, empty loop control blocks, etc.) purely. With it, it can additionally express placeholder semantics. The former is for machines and must-haves, which is equivalent to empty curly braces in other languages. The latter is for humans and is unnecessary and can be expressed by comments, but because Python designed this sentence, This usage is sometimes quite convenient.


Yeah, we made it to the end. Hope you enjoy it and have a basic idea about why Python has a "pass" statement?