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
sta
If you want to import a module, if it exists, and ignore importing it, if that module does not exists, you can use the below code:
try:
import a_module
except ImportError:
pass
#rest of your code
If you avoid writing the pass statement and continue writing the rest of your code, a IndentationError would be raised, since the lines after opening the except block are not indented
Suppose you are designing a new class with some methods that you don't want to implement, yet.
class MyClass(object):
def meth_a(self):
pass
def meth_b(self):
print "I'm meth_b"
If you were to leave out the pass
, the code wouldn't run.
You would then get an:
IndentationError: expected an indented block
To summarize, the pass
statement does nothing particular, but it can act as a placeholder, as demonstrated here.
You can say that pass means NOP (No Operation) operation. You will get a clear picture after this example :-
C Program
#include<stdio.h>
void main()
{
int age = 12;
if( age < 18 )
{
printf("You are not adult, so you can't do that task ");
}
else if( age >= 18 && age < 60)
{
// I will add more code later inside it
}
else
{
printf("You are too old to do anything , sorry ");
}
}
Now how you will write that in Python :-
age = 12
if age < 18:
print "You are not adult, so you can't do that task"
elif age >= 18 and age < 60:
else:
print "You are too old to do anything , sorry "
But your code will give error because it required an indented block after elif . Here is the role of pass keyword.
age = 12
if age < 18:
print "You are not adult, so you can't do that task"
elif age >= 18 and age < 60:
pass
else:
print "You are too old to do anything , sorry "
Now I think its clear to you.
as the book said, I only ever use it as a temporary placeholder, ie,
# code that does something to to a variable, var
if var == 2000:
pass
else:
var += 1
and then later fill in the scenario where var == 2000
Honestly, I think the official Python docs describe it quite well and provide some examples:
The pass statement does nothing. It can be used when a statement is required syntactically but the program requires no action. For example:
>>> while True: ... pass # Busy-wait for keyboard interrupt (Ctrl+C) ...
This is commonly used for creating minimal classes:
>>> class MyEmptyClass: ... pass ...
Another place pass can be used is as a place-holder for a function or conditional body when you are working on new code, allowing you to keep thinking at a more abstract level. The pass is silently ignored:
>>> def initlog(*args): ... pass # Remember to implement this! ...
pass is used to avoid indentation error in python If we take languages like c,c++,java they have braces like
if(i==0)
{}
else
{//some code}
But in python it used indentation instead of braces so to avoid such errors we use pass. Remembered as you were playing a quiz and
if(dont_know_the_answer)
pass
Example program,
for letter in 'geeksforgeeks':
pass
print 'Last Letter :', letter