python的pass语句
Prerequisite: pass statement in Python
先决条件: Python中的pass语句
An empty function is a function that does not contain any statement within its body. If you try to write a function definition without any statement in python – it will return an error ("IndentationError: expected an indented block").
空函数是在函数体内不包含任何语句的函数。 如果您尝试在python中编写不带任何语句的函数定义–它将返回错误(“ IndentationError:期望缩进的块”)。
Consider the given code,
考虑给定的代码,
# python code to demonstrate example of
# pass statement
# an empty function definition without any statement
def myfunc():
# main code
print("calling function...")
myfunc()
print("End of the program")
Output
输出量
File "/home/main.py", line 8
print("calling function...")
^
IndentationError: expected an indented block
See the output – there is no statement in the definition part of the function myfunc(), so python compiler considers the next statement (which is a “print” statement in this program) as a statement of the function definition. Thus, an error "IndentationError: expected an indented block" occurs.
看到输出–函数myfunc()的定义部分中没有语句,因此python编译器将下一条语句(此程序中的“ print”语句)视为函数定义的语句。 因此,发生错误“ IndentationError:预期缩进的块” 。
用“ pass”语句定义一个空函数 (Defining an empty function with "pass" statement)
If there is no statement in the function i.e. we want to create it as an empty function – we can use pass statement. As we have discussed in the earlier post (pass statement in python) that, a pass statement is a null statement and it does nothing.
如果函数中没有语句,即我们想将其创建为空函数,则可以使用pass语句 。 正如我们在较早的文章( python中的pass语句)中所讨论的那样,pass语句是null语句,它什么也不做。
带pass语句的空函数的Python代码 (Python code for an empty function with pass statement)
# python code to demonstrate example of
# pass statement
# an empty function definition with pass statement
def myfunc():
pass
# another function having statement
def urfunc():
print("This is your function")
# main code
print("calling function...")
# calling functions
myfunc()
urfunc()
print("End of the program")
Output
输出量
calling function...
This is your function
End of the program
See the output – there is no error in the program, it compiled successfully. We just wrote a pass statement with the empty function myfunc().
看到输出–程序中没有错误,编译成功。 我们只是用空函数myfunc()编写了pass语句 。
翻译自: https://www.includehelp.com/python/define-an-empty-function-using-pass-statement.aspx
python的pass语句