Home >Backend Development >Python Tutorial >What is the function of pass in python

What is the function of pass in python

hzc
hzcOriginal
2020-06-13 12:04:517179browse

What is the function of pass in python

The function of pass in python is:

  1. Empty statement do nothing

  2. Guaranteed format

  3. Complete guarantee of complete semantics

Take the if statement as an example, in c or c/java:

if(true)
;    //do nothing
else
{
    //do something
}

Corresponding to python, you should write it like this

if true:
    pass #do nothing
else:
    #do something

The role of the pass statement in the function
When you are writing a program, the execution statement part of the idea has not yet been completed. At this time, you can use the pass statement to occupy the place, or it can be used as a mark, which is the code to be completed later. For example, as follows:

def iplaypython():
    pass

Define a function iplaypython, but the function body has not been completed yet, and it cannot be left empty without writing content, so pass can be used instead to occupy a position.

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, each iteration No operation is required, 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 will do nothing if it is empty. At this time, python It will enter an infinite loop.

Recommended tutorial: "Python Tutorial"

The above is the detailed content of What is the function of pass in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn