I’m just wondering what the purpose of the pass statement is and how its used. The answers I’ve found are vague saying it does nothing- so what is it used for? Any examples?
Thanks!
Dave
I’m just wondering what the purpose of the pass statement is and how its used. The answers I’ve found are vague saying it does nothing- so what is it used for? Any examples?
Thanks!
Dave
It’s mostly used as a place holder, for example, if you’re writing some code and you know you’ll use a function (let’s say get_totals
) you could put the function in without any other code and the IDE would not throw a hissy fit:
def get_totals():
pass
In Python, every block of code must contain at least one statement, whether it is a conditional block, a function definition, or a class definition. So, if you are writing some code, and you have created a block for which you have not yet developed any real content, you can temporarily include a pass
statement as a placeholder, as @rob42 has mentioned, in order to avoid a SyntaxError
, since a statement is required there syntactically.
See Python Reference: 7. Simple statements: 7.4. The pass statement.
Later on, you can replace the pass
statement with some useful code.
There are a variety of use cases for pass, though their number has
dwindled over time as some language constructs have grown ways to
omit previously mandatory sections. A common example is when you’re
prototyping a program and you want to sketch out a high-level
outline but leave some function calls unimplemented initially. The
language doesn’t (yet, as far as I know anyway) have a way for you
to write a function with absolutely no content:
def foo(bar):
# TODO: make this work
pass
# not yet implemented, will eventually frobnicate the trunnion
foo(23)
It also used to be common in exception-oriented error handling to
use pass when you wanted to simply ignore a particular condition
(but since Python 3.4 it’s clearer to use suppress instead):
try:
quux.plugh(gralpy)
except NameError:
pass
These are of course far from the only places you might use pass,
just a couple that come to mind for demonstration purposes. Pretty
much anywhere the language requires a statement, you can use pass as
a no-op.
I’d agree with both of those, with the clarification that the language DOES have a way to write a function with no content, and it’s exactly what you put above The
pass
statement is a syntactic construct that has no functionality, no behaviour. It does nothing. So the function really truly has no body. (Though any function will have an implicit return None
at the end of it, so this function is a tiny stub that does nothing but return None
.)
In my personal usage, I use pass
when the intention is that this should definitely have no content in it. As well as the “except NameError: pass” example (or any other error you want to suppress), there’s another situation where this comes up fairly often, and that’s classes that don’t need any special behaviour:
class InsufficientChocolateException(Exception): pass
def write_code():
if chocolate < 250:
raise InsufficientChocolateException("Need a minimum of 250g chocolate for this action")
Occasionally I’ll use pass
inside an if/elif/else tree:
if line == "": pass
elif line == "Line Type #1": handle_one()
elif line == "Line Type #2": handle_two()
else: handle_generic()
But if it’s a stub that should be expanded on later, I instead use ellipsis.
def handle_frobnostication(x, y, level=1):
...
This is perfectly legal syntax, and is equivalent to putting a bare literal or something in there - it won’t actually DO anything, but it looks like something’s incomplete, so a subsequent developer will know the intention.
The documentation for pass
says:
pass
is a null operation — when it is executed, nothing happens. It is useful as a placeholder when a statement is required syntactically, but no code needs to be executed, for example: …
followed by two examples.
If that’s not clear, is there something we can do to improve it?
Docstrings, added since 1.0, count as a statement and hence have somewhat replaced pass
for def
statements.
>>> def no_pass():
... "Docstrings count as a statement, no pass needed"
...
>>> no_pass()
>>>
pass
is a placeholder. Here’s a rare example of using pass
in actual code:
Code
def get_last(it):
for last in it:
pass
return last
get_last(range(101)))
# 100
Notes
In many cases you could replace pass
with ...
, the Ellipsis
constant. However, you can’t always do the opposite. I’m unsure of an official, pythonic idiom, but I tend to use pass
to explicitly express “no more code is required here.” I use ...
to express “missing code”, which is cute after all.
Yeah, I agree (with the understanding that “missing” often means “still-to-come”). It’s extremely handy.