詹惠儿

2018-11-06   阅读量: 862

数据分析师 Python数据分析 Python编程

如何在Python中编写一个空函数 - 传递语句?

扫码加入数据分析学习群

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

// C / C ++ / Java中的空函数
void fun(){}

然而在Python中,如果我们在Python中编写类似下面的内容,则会产生编译器错误。

# Incorrect empty function in Python
def fun(): 

输出:

IndentationError:预期缩进块

在Python中,要编写空函数,我们使用pass语句。pass是Python中的一个特殊语句,它什么都不做。它只能作为虚拟语句。

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

pass

我们也可以在空语句中使用pass。

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

pass

我们可以在空if语句中使用pass。

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

0.0000 0 2 关注作者 收藏

评论(0)


暂无数据

推荐课程

推荐帖子