In Python, pass is a null statement and it does nothing when executed. Despite not doing anything, pass does have a purpose.
As you know, Python uses indentation to define a block of code (such as the body of if, for, while etc.). These blocks cannot be empty. For example,
for i in range(1, 5):
# Run this code
If you run this code, you'll get an IndentationError because Python expects at least one indented statement after the colon :. Note that comments are not considered statements and are completely ignored by the Python interpreter.
In such cases, we can use pass as a placeholder.
Its syntax is:
pass
Example: pass Statement
is_valid = True
if is_valid:
pass
else:
print("Login invalid. Redirect to form.")
Here, we're using pass inside the if statement so that the program doesn't raise an error.
Generally, a pass statement is used when we're unsure what code to write in a block yet. It lets us define the structure of our program first and fill in the implementation later.
This becomes even more useful when we start working with functions and classes.
# Implement token calculation later
def calculate_token():
pass
By the way, you can use the ellipsis (...) or any other literal instead of pass as the placeholder, but it's not standard practice.
def calculate_token():
...