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

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

//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

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

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

如果else语句, 我们可以使用pass in空。

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

如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请发表评论。

首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。

来源:

https://www.srcmini02.com/68880.html

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_35189.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?