如何在Python–pass语句中编写空函数?

在C/C++和Java中,我们可以编写如下空函数

null
// An empty function in C/C++/Java
void fun() {  }

在Python中,如果我们在Python中编写类似于以下内容的代码,就会产生编译器错误。

# Incorrect empty function in Python
def fun():


输出:

IndentationError: expected an indented block

在Python中,为了编写空函数,我们使用 通过 陈述pass是Python中的一个特殊语句,它什么都不做。它只是一个伪语句。

# Correct way of writing empty function
# in Python
def fun():
pass


我们也可以使用pass-in-empty while语句。

# Empty loop in Python
mutex = True
while (mutex = = True ) :
pass


我们可以使用pass-in空if-else语句。

# Empty in if/else in Python
mutex = True
if (mutex = = True ) :
pass
else :
print ( "False" )


本文由 希瓦姆·古普塔 。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请发表评论

© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享