- Pass is a keyword.
- The pass statement is basically a null statement.
- Pass Statement is used when a statement is required syntactically.
- Nothing happens when the pass statement is executed.
- The pass statement is discarded during the byte-compile phase.
- The major difference between pass and comment is that comment is ignored by the interpreter whereas pass is not ignored.
- Empty code is not allowed in loops, function definitions, class definitions, or in if statements. So we execute a pass statement there, which prevents us from getting an error.
- Pass can be used as a placeholder when code is to be added later. If there’s a function or a loop that we would like to implement in the future, we might construct an empty function or loop first with an empty body. Since this is syntactically illegal, we use the null statement pass as a placeholder to comply with the syntax requirement.
Pass Statement With Function:
01. Example
In this example, we are calling the secondFunction() because sometime programmer write statements later inside firstFunction() later as per the requirement of the project. But it will throw error because we can’t leave firstFunction() like this.
def firstFunction(): def secondFunction(): print("Second Function Is Working.") secondFunction()
Output:
File "<string>", line 3 def secondFunction(): ^ IndentationError: expected an indented block
02. Example
Now this is working by just adding one keyword in firstFunction() that is pass keyword.
def firstFunction(): pass def secondFunction(): print("Second Function Is Working.") secondFunction()
Output:
Second Function Is Working.
03. Example
class firstFunction: print("Before Pass Statement.") pass print("After Pass Statement.") class secondFunction: print("Second Function Is Working.")
Output:
Before Pass Statement.
After Pass Statement.
Second Function Is Working.
Pass Statement With Class:
01. Example
Again similar, to the concept we are trying with class. When we execute the below code it will throw the error which is described below. Because firstClass doesn’t have any statements inside and because of that secondClass is not executable. We can’t run an empty class according to this analysis.
class firstClass: class secondClass: print("Second Class Is Working.")
Output:
File "<string>", line 3 class secondClass: ^ IndentationError: expected an indented block
02. Example
Now this is working again by just adding one keyword in firstClass that is pass keyword.
class firstClass: print("Before Pass Statement.") pass print("After Pass Statement.") class secondClass: print("Second Class Is Working.")
Output:
Before Pass Statement.
After Pass Statement.
Second Class Is Working.
Pass Statement With For Loops:
01. Example
Again checking with For Loop empty statement. but it is showing error as covered in below code snippet. Here for loop is totally empty and these are no statement, we cann’t do that but sometime we reuired empty for loop. So pass statement is used to avoid from these type of error.
stringArray = ['Mango','Apple','Orange','Grapes'] for data in stringArray: print("For Loop Is Successfully Executed.")
Output:
File "<string>", line 5 print("For Loop Is Successfully Executed.") ^ IndentationError: expected an indented block
02. Example
As pass statement has been placed inside for loop. Now there are no errors and last print statement also executing.
stringArray = ['Mango','Apple','Orange','Grapes'] for data in stringArray: pass print("For Loop Is Successfully Executed.")
Output:
For Loop Is Successfully Executed.
Pass Statement With If-Else:
01. Example
If-else statements also get an error when we are not adding any statement below if statement because at least one correct statement is required but sometimes programmers don’t want to execute any statement.
stringArray = ['Mango','Apple','Orange','Grapes'] for data in stringArray: if(data == 'Apple' or data == 'Mango'): else: print("Fruit Name: ",data)
Output:
File "<string>", line 6 else: ^ IndentationError: expected an indented block
02. Example
As you can see when ‘Apple’ or ‘Mango’ is compared with data which we are extracting from stringArray, it is just passing, there is no error.
stringArray = ['Mango','Apple','Orange','Grapes'] for data in stringArray: if(data == 'Apple' or data == 'Mango'): pass else: print("Fruit Name: ",data)
Output:
Fruit Name: Orange Fruit Name: Grapes