“通过"与“返回无"相同在 Python 中? [英] "pass" same as "return None" in Python?

查看:30
本文介绍了“通过"与“返回无"相同在 Python 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经学习python大约一周了,下面是问题:

I have been learning python about a week now, below is the question:

代码

def Foo():
    pass

def Bar():
    return None

使用

a = Foo()
print(a)
# None
b = Bar()
print(b)
# None

问题:1.当我们已经有return None时,为什么还需要pass?有没有return None不能处理而pass可以处理的情况?

Question: 1. Why do we need pass when we already have return None? Is there some scenario that return None can not handle but pass can?

推荐答案

在像 Python 这样的语言中,你有两种类型的事物"被评估:表达式和语句.表达式是,嗯,表达式,它评估某些东西.例如 11+1f(x) 都是表达式,求值.

In languages like Python, you have two types of 'things' being evaluated: expressions and statements. Expressions are, well, expressions, which evaluate to something. For instance 1, 1+1, or f(x) are all expressions, evaluating to something.

相比之下,语句不会返回"任何东西,而是作用于环境".例如,x=3 将变量 x 设置为值 3.def f(x): return x + 1f 的值设置为一个函数.这不会评估"任何东西,它会修改评估事物的当前环境.

By comparison, statements are not 'returning' anything, instead, they 'act on the environment'. For instance, x=3 sets the variable x to the value 3. def f(x): return x + 1 sets the value of f to a function. That doesn't 'evaluate' to anything, that modifies the current environment where things are evaluated.

考虑到这一点,您有时需要一个空语句.出于语法原因,Python 中有些地方您需要一个语句,但是如果您不希望这里发生任何事情,您可能想表达所发生的事情正是无".一种方法是对表达式求值而不对其进行任何操作:

With this in mind, you sometimes need an empty statement. For syntactic reasons, there are places in Python where you need a statement, but if you don't want anything to happen here, you might want to express that what happens is precisely 'nothing'. One way would be to evaluate an expression and do nothing with it:

def f():
    1

但随便看一眼可能会表明该函数返回 1:相反,您可以使用 pass 作为 explicitely 说明:什么都不做":

But a casual glance at that might suggests that this function returns 1: instead, you can use pass as explicitely stating: 'do nothing':

def f():
    pass

现在,还有一个问题:我之前提到过 f(x) 是一个表达式.但是在上面的两个示例中,我没有放置任何 return,那么这些计算结果是什么?答案是:除非另有说明,否则函数调用返回(即评估为)None.所以:

Now, there's another catch: I mentioned earlier that f(x) is an expression. But in the two examples above, I've not put any return, so what do these evalute to ? The answer is: a function call returns (i.e. evaluates to) None, unless specified otherwise. So:

def f():
    pass

def g(x):
    if x > 2:
        return x

def h():
    return None

f() # None
g(3) # 3 -> we explicitely say `return x`
g(1) # None -> no return specified for that path
h() # None -> we explicitely say `return None`

其他人提到 pass 可以在其他地方使用(例如在空类中,在异常处理中......).这样做的原因是所有这些都需要 some 语句,而 pass 允许指定一个空的.这与函数返回 None 除非另有说明的事实相结合,导致您观察到的行为(尽管它们在概念上不同).

Others have mentioned that pass can be used in other places (e.g. within empty classes, in exception handling...). The reason for this is that all of these require some statement, and pass allows to specify an empty one. This, combined with the fact that a function returns None except specified otherwise, leads to the behaviour you observe (although they are conceptually different).

这篇关于“通过"与“返回无"相同在 Python 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆