What Is "pass" in Python?

In Python, the pass statement does nothing when it's executed. It is typically used as a placeholder (for future code) in places where a statement is required syntactically (such as in loops, function and class definitions, if statements, etc.).

For example, the following function does nothing when executed; it is merely meant as a placeholder:

def foo():
  pass

print(foo()) # None

Consider the following class that has no methods yet, and is defined as a placeholder:

class Foo:
    pass

foo = Foo()
print(foo) # <__main__.Foo object at ...>

You could use pass in an if statement as well, where the code for it might not be ready yet:

n = 0

if n == 0:
    pass
else:
    print("n is not 0")

Similarly, you can use the pass statement to act as a placeholder in other places where empty code is not allowed, so that the code execution can pass without raising any errors.


This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.