[英]What is the difference between Pass and None in Python
I would personally like to know the semantic difference between using Pass and None. 我个人想知道使用Pass和None之间的语义差异。 I could not able to find any difference in execution.
我无法找到任何执行上的差异。
PS: I could not able to find any similar questions in SO. PS:我无法在SO中找到任何类似的问题。 If you find one, please point it out.
如果您找到一个,请指出。
Thanks! 谢谢!
pass
is a statement . pass
是一个声明 。 As such it can be used everywhere a statement can be used to do nothing. 因此,它可以在任何地方使用,语句可以用来什么都不做。
None
is an atom and as such an expression in its simplest form. None
是一个原子 ,因此它是最简单形式的表达 。 It is also a keyword and a constant value for “nothing” (the only instance of the NoneType
). 它也是“nothing”的关键字和常量值(
NoneType
的唯一实例)。 Since it is an expression, it is valid in every place an expression is expected. 因为它是一个表达式,所以在每个地方都有一个表达式是有效的。
Usually, pass
is used to signify an empty function body as in the following example: 通常,
pass
用于表示空函数体,如下例所示:
def foo():
pass
This function does nothing since its only statement is the no-operation statement pass
. 此函数不执行任何操作,因为它的唯一语句是无操作语句
pass
。
Since an expression is also a valid function body, you could also write this using None
: 由于表达式也是一个有效的函数体,您也可以使用
None
来编写它:
def foo():
None
While the function will behave identically, it is a bit different since the expression (while constant) will still be evaluated (although immediately discarded). 虽然函数的行为相同,但它有点不同,因为表达式(虽然常量)仍将被评估(尽管立即被丢弃)。
In simple terms, None
is a value that you can assign to a variable that signifies emptiness. 简单来说,
None
是可以分配给表示空虚的变量的值。 It can be useful as a default state: 它可以作为默认状态使用:
a = None
def f():
a = 5
f()
pass
is a statement that is like a nop. pass
是一个像nop的声明。 It can be useful when you are defining function stubs, for instance: 在定义函数存根时,它非常有用,例如:
def f():
pass
In C-like languages, you would be able to define empty functions by simply putting nothing between the braces void f() { }
, but since Python uses indentation instead of braces to define blocks, you must put something in the body, and pass
is the idiomatic thing to put there. 在类C语言中,您可以通过简单地在大括号
void f() { }
之间放置任何内容来定义空函数,但由于Python使用缩进而不是大括号来定义块,所以必须在主体中放置一些东西 ,然后pass
是放在那里的惯用之物。
That's absolute difference between pass
and None
pass
和None
之间的绝对差异
The pass
(without upper case P) : pass
(没有大写P) :
Because python be the indent base language, so if you define a new method, you should have some code after that. 因为python是缩进基本语言,所以如果你定义一个新方法,那么你应该有一些代码。
def method_a():
some_thing = 1 # Have to do some thing
If not, an exception should be raised so you could use the pass
keyword for hacks this problem. 如果没有,则应该引发异常,以便您可以使用
pass
关键字来解决此问题。
def method_a():
pass # Do nothing
The None
: None
:
So very different, the None
keyword has a little bit same to the null
keywords from another language like Java or C. That may be the empty data or not assign data like that. 如此非常不同,
None
关键字与来自另一种语言(如Java或C)的null
关键字略微相同。可能是空数据或不分配这样的数据。
[] == None
null == None
() == None
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.