Usage of Python pass:
Empty statement do nothing
Ensure complete format
Ensure complete semantics
Take if statement as an example, in c or c++/Java:
if(true) ; //do nothing else { //do something }
Corresponding to Python, it should be written like this :
if true: pass #do nothing else: #do something
1 The role of the pass statement in the function
When you are writing a program, the execution statement part of the idea has not been completed. At this time, you can use the pass statement to occupy a place, or it can also be used as a mark. This is code to be completed later. For example, as follows:
def iplaypython(): pass
Define a function iplaypython, but the function body part is not completed yet, and it cannot be left empty without writing content, so you can use pass instead to occupy a place.
2 The role of pass statement in loops
pass is also often used to write an empty body for compound statements. For example, if you want an infinite loop of while statement, no operation is required for each iteration. You can write like this:
while True: pass
The above is just an example. In reality, it is best not to write such code, because the execution code block is pass, which means it is empty and does nothing. At this time, python will enter an infinite loop.
Thanks for reading, I hope it can help everyone. For more related articles, please pay attention to the PHP Chinese website (www.php.cn)!