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?
You can say that pass means NOP (No Operation) operation. You will get a clear picture after this example :-
C Program
Now how you will write that in Python :-
But your code will give error because it required an indented block after elif . Here is the role of pass keyword.
Now I think its clear to you.
if you don't know what you are going to put in a certain code block
I like to use it when stubbing out tests too. I often times am aware of what i would liek to test but dont' quite know how to do it. Testing example looks like what sebastian_oe suggested
Honestly, I think the official Python docs describe it quite well and provide some examples:
Besides its use as a placeholder for unimplemented functions,
pass
can be useful in filling out an if-else statement ("Explicit is better than implicit.")Of course, in this case, one would probably use
return
instead of assignment, but in cases where mutation is desired, this works best.The use of
pass
here is especially useful to warn future maintainers (including yourself!) not to put redundant steps outside of the conditional statements. In the example above,flag
is set in the two specifically mentioned cases, but not in theelse
-case. Without usingpass
, a future programmer might moveflag = True
to outside the condition—thus settingflag
in all cases.Another case is with the boilerplate function often seen at the bottom of a file:
In some files, it might be nice to leave that there with
pass
to allow for easier editing later, and to make explicit that nothing is expected to happen when the file is run on its own.Finally, as mentioned in other answers, it can be useful to do nothing when an exception is caught:
A common use case where it can be used 'as is' is to override a class just to create a type (which is otherwise the same as the superclass), e.g.
So you can raise and catch
Error
exceptions. What matters here is the type of exception, rather than the content.The pass statement in Python is used when a statement is required syntactically but you do not want any command or code to execute.
The pass statement is a null operation; nothing happens when it executes. The pass is also useful in places where your code will eventually go, but has not been written yet (e.g., in stubs for example):
`Example:
This will produce following result:
The preceding code does not execute any statement or code if the value of letter is 'h'. The pass statement is helpful when you have created a code block but it is no longer required.
You can then remove the statements inside the block but let the block remain with a pass statement so that it doesn't interfere with other parts of the code.