In Python, the pass
keyword is an entire statement in itself. This statement doesn’t do anything: it’s discarded during the byte-compile phase. But for a statement that does nothing, the Python pass
statement is surprisingly useful.
Sometimes pass
is useful in the final code that runs in production. More often, pass
is useful as scaffolding while developing code. In specific cases, there are better alternatives to doing nothing.
In this tutorial, you’ll learn:
- What the Python
pass
statement is and why it’s useful - How to use the Python
pass
statement in production code - How to use the Python
pass
statement as an aid while developing code - What the alternatives to
pass
are and when you should use them
Free Bonus: Click here to get a Python Cheat Sheet and learn the basics of Python 3, like working with data types, dictionaries, lists, and Python functions.
Python pass
Statement: Syntax and Semantics
In Python syntax, new indented blocks follow a colon character (:
). There are several places where a new indented block will appear. When you start to write Python code, the most common places are after the if
keyword and after the for
keyword:
>>> for x in [1, 2, 3]:
... y = x + 1
... print(x, y)
...
1 2
2 3
3 4
After the for
statement is the body of the for
loop, which consists of the two indented lines immediately following the colon.
In this case, there are two statements in the body that are repeated for each value:
y = x + 1
print(x, y)
The statements inside this type of block are technically called a suite in the Python grammar. A suite must include one or more statements. It can’t be empty.
To do nothing inside a suite, you can use Python’s special pass
statement. This statement consists of only the single keyword pass
. While you can use pass
in many places in Python, it’s not always useful:
>>> if 1 + 1 == 2:
... print("math is ok")
... pass
... print("but this is to be expected")
...
math is ok
but this is to be expected
In this if
statement, removing the pass
statement would keep the functionality the same and make your code shorter. You might be wondering why the Python syntax includes a statement that tells the interpreter to do nothing. Couldn’t you achieve the same result by not writing a statement at all?
In some cases, explicitly telling Python to do nothing serves an important purpose. For example, because the pass
statement doesn’t do anything, you can use it to fulfill the requirement that a suite include at least one statement:
>>> if 1 + 1 == 3:
...
File "<stdin>", line 2
^
IndentationError: expected an indented block
Even if you don’t want to add any code inside the if
block, an if
block with no statement creates an empty suite, which is invalid Python syntax.
To fix this, you can use pass
:
>>> if 1 + 1 == 3:
... pass
...
Now, thanks to pass
, your if
statement is valid Python syntax.
Temporary Uses of pass
There are many situations in which pass
can be useful to you while you’re developing, even if it won’t appear in the final version of your code. Much like scaffolding, pass
can be handy for holding up the main structure of your program before you fill in the details.
It might sound strange to write code that will be deleted later, but doing things this way can accelerate your initial development.
Future Code
Read the full article at https://realpython.com/python-pass/ »
[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]
from Real Python
read more
No comments:
Post a Comment