I am in the process of learning Python and I have reached the section about the pass
statement. The guide I'm using defines it as being a Null
statement that is commonly used as a placeholder.
I still don't fully understand what that means though. Can someone show me a simple/basic situation where the pass
statement would be used and why it is needed?
The pass statement does nothing. It can be used when a statement is required syntactically but the program requires no action.
Here's an example where I was extracting particular data from a list where I had multiple data types (that's what I'd call it in R-- sorry if it's the wrong nomenclature) and I wanted to extract only integers/numeric and NOT character data.
The data looked like:
I wanted to remove all alphabetical characters, so I had the machine do it by subsetting the data, and "passing" over the alphabetical data:
Python has the syntactical requirement that code blocks (after
if
,except
,def
,class
etc.) cannot be empty. Empty code blocks are however useful in a variety of different contexts, such as in examples below, which are the most frequent use cases I have seen.Therefore, if nothing is supposed to happen in a code block, a
pass
is needed for such a block to not produce anIndentationError
. Alternatively, any statement (including just a term to be evaluated, like theEllipsis
literal...
or a string, most often a docstring) can be used, but thepass
makes clear that indeed nothing is supposed to happen, and does not need to be actually evaluated and (at least temporarily) stored in memory.Ignoring (all or) a certain type of
Exception
(example fromxml
):Note: Ignoring all types of raises, as in the following example from
pandas
, is generally considered bad practice, because it also catches exceptions that should probably be passed on to the caller, e.g.KeyboardInterrupt
orSystemExit
(or evenHardwareIsOnFireError
– How do you know you aren't running on a custom box with specific errors defined, which some calling application would want to know about?).Instead using at least
except Error:
or in this case preferablyexcept OSError:
is considered much better practice. A quick analysis of all python modules I have installed gave me that more than 10% of allexcept ...: pass
statements catch all exceptions, so it's still a frequent pattern in python programming.Deriving an exception class that does not add new behaviour (e.g. in
scipy
):Similarly, classes intended as abstract base class often have an explicit empty
__init__
or other methods that subclasses are supposed to derive. (e.g.pebl
)Testing that code runs properly for a few test values, without caring about the results (from
mpmath
):In class or function definitions, often a docstring is already in place as the obligatory statement to be executed as the only thing in the block. In such cases, the block may contain
pass
in addition to the docstring in order to say “This is indeed intended to do nothing.”, for example inpebl
:In some cases,
pass
is used as a placeholder to say “This method/class/if-block/... has not been implemented yet, but this will be the place to do it”, although I personally prefer theEllipsis
literal...
in order to strictly differentiate between this and the intentional “no-op” in the previous example. For example, if I write a model in broad strokes, I might writewhere others might have
before
as a reminder to fill in the
update_agent
function at a later point, but run some tests already to see if the rest of the code behaves as intended. (A third option for this case israise NotImplementedError
. This is useful in particular for two cases: Either “This abstract method should be implemented by every subclass, there is no generic way to define it in this base class”, or “This function, with this name, is not yet implemented in this release, but this is what its signature will look like”)The best and most accurate way to think of
pass
is as a way to explicitly tell the interpreter to do nothing. In the same way the following code:means "if I call the function foo(x, y), sum the two numbers the labels x and y represent and hand back the result",
means "If I call the function bar(), do absolutely nothing."
The other answers are quite correct, but it's also useful for a few things that don't involve place-holding.
For example, in a bit of code I worked on just recently, it was necessary to divide two variables, and it was possible for the divisor to be zero.
will, obviously, produce a ZeroDivisionError if b is zero. In this particular situation, leaving c as zero was the desired behavior in the case that b was zero, so I used the following code:
Another, less standard usage is as a handy place to put a breakpoint for your debugger. For example, I wanted a bit of code to break into the debugger on the 20th iteration of a for... in statement. So:
with the breakpoint on pass.
pass
in Python basically does nothing, but unlike a comment it is not ignored by interpreter. So you can take advantage of it in a lot of places by making it a place holder:1: Can be used in class
2: Can be use in loop and conditional statements:
3: Can be used in function :
pass
is mostly used when programmer does not want to give implementation at the moment but still wants to create a certain class/function/conditional statement which can be used later on. Since the Python interpreter does not allow for blank or unimplemented class/function/conditional statement it gives an error:pass
can be used in such scenarios.Suppose you are designing a new class with some methods that you don't want to implement, yet.
If you were to leave out the
pass
, the code wouldn't run.You would then get an:
To summarize, the
pass
statement does nothing particular, but it can act as a placeholder, as demonstrated here.