In python ‘pass‘ is a keyword as well as a complete statement in itself. There could be a situation while programming that you want to implement some code in the form of a function or a loop but in future i.e. some time later. Choice is that you can omit it all together, but you want something better.
It would be better if you are allowed to partially implement it in such a way that it reminds you that you are required to implement some piece of code at some place –
- if-else clause or
- inside some function.
Python provides a special keyword ‘pass’ to support that.
Example code for Python ‘pass’ statement
for x in range(1,10): print(x) if(x==5): pass #command that tells python nothing is to be done else: print(x*x)
Another example code for Python ‘pass’ statement
def example(m): if m%2==0: print("Square of m: ",m*m) else: pass # What is to be done when m is an odd number is # left for future implementation # Another Example where entire function is left to be implemented later on: def afunction(somevalue): pass
Might not appeal that it is a very useful statement and you might just feel we can do without it, but believe me it is quite useful and when used at the right time and at the right place it will give you a peace of mind.
Conclusion – Key notes
- In Python you can not create any loops or function definitions without body ( code inside them, or a empty blocks to put simply). Sometimes you want to create some loops or functions that for some reason are not doing anything but you want them to exist as placeholders reminding you that you need to implement them in future.
- The Python ‘pass’ statement or a keyword can be used as a kind of placeholder. Later you can replace it with some valid required code.
- Python ‘pass’ statement is not ignored like a comment but at the same time it does not produces an error also.