Have you ever imagined code that does absolutely nothing? Python has one, and it's called the pass statement. Some of you may be wondering "if it does nothing, why does it exist?". Well, even though it does nothing, this statement still has a job in your Python code. What is it? Let's find out.
About the Pass Statement
The pass statement in Python is a special statement you can only use inside a compound statement.
What is a compound statement?
According to the Python documentation, a compound statement is a statement made up of several other statements. Put simply, it's a statement that needs a body before you can use it. Examples are if branching, the for and while loops, a def function definition, and others.
In Python grammar the body of a compound statement is called a suite. There are rules you have to follow when you build a suite, and one of them is that a suite can't be empty. That's why you can't write an empty branch, an empty loop, or an empty function declaration.
❌ This syntax will error in Python
if 1 > 0:
for i in range(10):
def fungsi_kosong():
By comparison, here is the same logic written in valid JavaScript
if (1 > 0)
for (let i=1; i<10; i++)
function fungsiKosong(){}
But what is the point? What reason would make anyone build an empty compound statement?
One reason is when you already know the steps to solve a problem, but you're still unsure how to implement them. When writing a function, people sometimes write the function name first and think about the implementation later.
That reason is exactly why Python needs a placeholder. Its job is to fill a compound statement whose suite can't be empty. One of these placeholders is the pass statement.
How to Write the Pass Statement
The pass statement can be written on its own, just like the break, continue, and return statements.
if 1 > 0:
pass
for i in range(10):
pass
def fungsi_kosong():
pass
Now if you run that code, it's guaranteed not to raise an error.
Because the pass statement is written as the body of a compound statement, you still have to watch the indentation.
Alternatives to the Pass Statement
Besides the pass statement, Python also has a few pieces of code that do nothing at all. Here are some of them.
A Constant on Its Own
In Python, a value, which in this case we call a constant, is useless when it stands alone. Even so, a constant on its own is still treated as a statement.
The code below is valid
if 1 > 0:
true
for i in range(10):
false
def fungsi_kosong():
"kode ini valid"
Ellipsis
The next way is to use the ellipsis, also known as the three dots (...). For your information, the ellipsis is an object in its own right in Python, with its own data type.
You can check its type in the interactive interpreter.
>>> type(...)
<class 'ellipsis'>
>>> e = ...
>>> type(e)
<class 'ellipsis'>
So you can use the ellipsis as a replacement for the pass statement too.
Docstring
Last, though there are actually other ways, is the docstring. So what is a docstring?
Briefly, a docstring is a special comment used to document program code. Its shape looks like this:
"""
ini adalah docstring
ini baris ke dua docstring
docstring bekerja layaknya komentar
oleh karena itu docstring tidak akan dieksekusi
"""
Docstrings are very useful for describing a function you're about to write. You can write a long description this way.
def hitung_luas_lingkaran(r):
"""
fungsi ini berguna untuk mendapatkan luas lingkaran
rumus: luas = pi * r * r
implementasi nanti
"""
What's Next?
So that's the pass statement. Even though it does nothing, it still has its uses. Maybe it'll come in handy.
That's all for this one. Next you'll look at default argument in Python 3.
See ya 👋