python的pass语句_Python pass语句–通过Python

python的pass语句

Dear learners, how is everything going? Hope that you’re learning well. In our previous tutorial, we learned about Python break and continue statement to control python loops. You can find that tutorial here. In this tutorial, we are going to learn about Python pass statement.

亲爱的学习者,一切进展顺利吗? 希望你学习得很好。 在上一教程中,我们了解了Python break and continue语句以控制python循环。 您可以在这里找到该教程。 在本教程中,我们将学习Python pass语句。

Python pass语句 (Python pass statement)

You can consider python pass statement as no operation statement. The difference between Python comments and pass statement is; comments are being eliminated while interpreting the program but pass statement does not. It consumes execution cycle like a valid statement. For example, for printing only the odd numbers from a list, our program flow will be:

您可以将python pass语句视为无操作语句。 Python注释和pass语句之间的区别是; 在解释程序时,注释将被消除,但pass语句则不会。 它像有效语句一样消耗执行周期。 例如,仅打印列表中的奇数,我们的程序流程将是:

List <- a list of number
for each number in the list:
	if the number is even,
		then, do nothing
	else 
		print odd number

Now if we convert the above things to python,

现在,如果我们将上述内容转换为python,

#Generate a list of number
numbers = [ 1, 2, 4, 3, 6, 5, 7, 10, 9 ]
#Check for each number that belongs to the list
for number in numbers:
        #check if the number is even
	if number % 2 == 0:
                #if even, then pass ( No operation )
                pass
	else:
                #print the odd numbers
		print (number),

The output will be

输出将是

>>> 
================== RESTART: /home/imtiaz/Desktop/pass1.py ==================
1 3 5 7 9
>>>

Suppose you need to implement many function one by one. But you have to check every function after implementing the function. Now if you leave the things like this:

假设您需要一个接一个地实现许多功能。 但是您必须在实现功能后检查每个功能。 现在,如果您留下这样的内容:

def func1():
        # TODO: implement func1 later
 
def func2():
        # TODO: implement func2 later
        
def func3(a):
        print (a)
 
func3("Hello")

Then, you will get IndentationError for this.

python output showing IndentationError, python pass statement

然后,您将为此获得IndentationError。

So, what you need to do is, add pass statement to each no-implemented function like this.

因此,您需要做的是将pass语句添加到这样的每个未实现的函数中。

def func1():
        pass # TODO: implement func1 later
 
def func2():
        pass # TODO: implement func2 later
        
def func3(a):
        print (a)
 
func3("Hello")

For the above code, you will get output like this

对于上面的代码,您将获得如下输出

================== RESTART: /home/imtiaz/Desktop/pass3.py ==================
Hello
>>>

为什么要使用Python pass语句 (Why should we use Python pass Statement)

Now, a question may come to your mind, why should we use Python pass statement? Actually it seems that, in our previous example, if we just comment-out the unimplemented functions, like below, we will still get our desired output.

现在,您可能会想到一个问题,为什么我们要使用Python pass语句? 实际上,似乎在我们之前的示例中,如果仅注释掉未实现的功能(如下所示),我们仍将获得所需的输出。

#def func1():
        # TODO: implement func1 later
 
#def func2():
        # TODO: implement func2 later
        
def func3(a):
        print (a)
 
func3("Hello")

But if you work with a huge python project, at one time, you may need something like pass statement. That’s why pass statement is introduced in Python.

但是,如果您一次处理一个庞大的python项目,则可能需要诸如pass语句之类的东西。 这就是为什么在Python中引入pass语句的原因。

That’s all for today. Hope that, you learned well about Python pass statement. Stay tuned for our next tutorial and for any confusion, feel free to use the comment box.

今天就这些。 希望您对Python pass语句了解得很好。 请继续关注我们的下一个教程,如有任何困惑,请随时使用评论框。

Reference: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/14240/python-pass-statement

python的pass语句

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值