我在学习Python的过程中,我已经到了关于pass语句的部分。我正在使用的指南将其定义为通常用作占位符的空语句。
但我还是不太明白这是什么意思。pass语句将被使用的简单/基本情况是什么?为什么需要它?
我在学习Python的过程中,我已经到了关于pass语句的部分。我正在使用的指南将其定义为通常用作占位符的空语句。
但我还是不太明白这是什么意思。pass语句将被使用的简单/基本情况是什么?为什么需要它?
当前回答
老实说,我认为Python官方文档描述得很好,并提供了一些示例:
The pass statement does nothing. It can be used when a statement is required syntactically but the program requires no action. For example: >>> while True: ... pass # Busy-wait for keyboard interrupt (Ctrl+C) ... This is commonly used for creating minimal classes: >>> class MyEmptyClass: ... pass ... Another place pass can be used is as a place-holder for a function or conditional body when you are working on new code, allowing you to keep thinking at a more abstract level. The pass is silently ignored: >>> def initlog(*args): ... pass # Remember to implement this! ...
其他回答
首先,如果你想写一个block,像这样:
if statement:
pass
for i in range(abc):
pass
def func():
pass
pass can是一个占位符。
其次,它可以让你与IDE“交流”: 当你想让你的IDE像这样递减缩进: 如果你的程序写在这里:
class abc(parent):
def __init__(self, params):
self.params=params
if d:
return
else:
return
# cursor in there
现在你的缩进计数是2,但你希望它在下一行是1。 你可以输入一个pass,你的程序是这样的:
class abc(parent):
def __init__(self, params):
self.params=params
if d:
return
else:
return
pass# cursor in there
并返回。它会让你快乐:
class abc(parent):
def __init__(self, params):
self.params=params
if d:
return
else:
return
pass
# cursor in there
现在缩进计数是1。
老实说,我认为Python官方文档描述得很好,并提供了一些示例:
The pass statement does nothing. It can be used when a statement is required syntactically but the program requires no action. For example: >>> while True: ... pass # Busy-wait for keyboard interrupt (Ctrl+C) ... This is commonly used for creating minimal classes: >>> class MyEmptyClass: ... pass ... Another place pass can be used is as a place-holder for a function or conditional body when you are working on new code, allowing you to keep thinking at a more abstract level. The pass is silently ignored: >>> def initlog(*args): ... pass # Remember to implement this! ...
在Python中,pass基本上什么都不做,但与注释不同的是,它不会被解释器忽略。所以你可以在很多地方利用它,把它变成占位符:
1:可以在课堂上使用
class TestClass:
pass
2:可以在循环语句和条件语句中使用:
if (something == true): # used in conditional statement
pass
while (some condition is true): # user is not sure about the body of the loop
pass
3:可用于功能:
def testFunction(args): # The programmer wants to implement the body of the function later
pass
当程序员暂时不想给出实现,但仍然想创建某个类/函数/条件语句以供以后使用时,通常使用Pass。由于Python解释器不允许出现空白或未实现的类、函数或条件语句,因此会给出一个错误:
IndentationError:期望一个缩进的块
Pass可以在这种情况下使用。
你可以说pass意味着一个NOP(无操作)操作。看完这个例子,你就会清楚了:
C程序
#include<stdio.h>
void main()
{
int age = 12;
if( age < 18 )
{
printf("You are not adult, so you can't do that task ");
}
else if( age >= 18 && age < 60)
{
// I will add more code later inside it
}
else
{
printf("You are too old to do anything , sorry ");
}
}
现在你要怎么用Python写这个:
age = 12
if age < 18:
print "You are not adult, so you can't do that task"
elif age >= 18 and age < 60:
else:
print "You are too old to do anything , sorry "
但是您的代码会给出一个错误,因为在elif之后需要一个缩进的块。下面是pass关键字的作用。
age = 12
if age < 18:
print "You are not adult, so you can't do that task"
elif age >= 18 and age < 60:
pass
else:
print "You are too old to do anything , sorry "
现在我想你们都清楚了。
在Python中,pass语句用于在语法上需要语句,但不希望执行任何命令或代码的情况。
pass语句是一个空操作;执行时什么也不发生。pass在你的代码最终要去的地方也很有用,但还没有写出来(例如,在存根中):
例子:
#!/usr/bin/python
for letter in 'Python':
if letter == 'h':
pass
print 'This is pass block'
print 'Current Letter :', letter
print "Good bye!"
这将产生以下结果:
Current Letter : P
Current Letter : y
Current Letter : t
This is pass block
Current Letter : h
Current Letter : o
Current Letter : n
Good bye!
如果字母的值是'h',上面的代码不会执行任何语句或代码。当您创建了一个代码块时,pass语句很有用,但不再需要它。
然后,您可以删除块中的语句,但让块保留pass语句,这样它就不会干扰代码的其他部分。