本文翻译自:How to use the pass statement?

I am in the process of learning Python and I have reached the section about the pass statement. 我正在学习Python,并且已经到达了关于pass语句的部分。 The guide I'm using defines it as being a Null statement that is commonly used as a placeholder. 我正在使用的指南将其定义为Null语句,通常用作占位符。

I still don't fully understand what that means though. 我仍然不完全明白那是什么意思。 Can someone show me a simple/basic situation where the pass statement would be used and why it is needed? 有人可以告诉我一个简单/基本的情况,在哪里使用pass语句,为什么需要它?


#1楼

参考:https://stackoom.com/question/wGQS/如何使用pass语句


#2楼

Suppose you are designing a new class with some methods that you don't want to implement, yet. 假设您正在使用尚未实现的某些方法设计一个新类。

class MyClass(object):def meth_a(self):passdef meth_b(self):print "I'm meth_b"

If you were to leave out the pass , the code wouldn't run. 如果您遗漏了pass ,则代码将无法运行。

You would then get an: 然后,您将获得:

IndentationError: expected an indented block

To summarize, the pass statement does nothing particular, but it can act as a placeholder, as demonstrated here. 总而言之, pass语句没有什么特别的,但是它可以充当占位符,如此处所示。


#3楼

if you don't know what you are going to put in a certain code block 如果您不知道要在特定代码块中放入什么

try:int(someuserinput)
except ValueError:pass

I like to use it when stubbing out tests too. 我也喜欢在进行测试时使用它。 I often times am aware of what i would liek to test but dont' quite know how to do it. 我经常知道自己喜欢测试什么,但不太了解该怎么做。 Testing example looks like what sebastian_oe suggested 测试示例看起来像sebastian_oe所建议的

class TestFunctions(unittest.TestCase):def test_some_feature(self):passdef test_some_other_feature(self):pass

#4楼

as the book said, I only ever use it as a temporary placeholder, ie, 如书中所述,我只曾将其用作临时占位符,即

# code that does something to to a variable, var
if var == 2000:pass
else:var += 1

and then later fill in the scenario where var == 2000 然后再填写var == 2000


#5楼

A common use case where it can be used 'as is' is to override a class just to create a type (which is otherwise the same as the superclass), eg 可以按原样使用的一种常见用例是重写一个类,仅是为了创建一个类型(否则与超类相同),例如

class Error(Exception):pass

So you can raise and catch Error exceptions. 因此,您可以引发并捕获Error异常。 What matters here is the type of exception, rather than the content. 这里重要的是异常的类型,而不是内容。


#6楼

Besides its use as a placeholder for unimplemented functions, pass can be useful in filling out an if-else statement ("Explicit is better than implicit.") 除了用作未实现函数的占位符外, pass还可用于填充if-else语句(“显式优于隐式。”)

def some_silly_transform(n):# Even numbers should be divided by 2if n % 2 == 0:n /= 2flag = True# Negative odd numbers should return their absolute valueelif n < 0:n = -nflag = True# Otherwise, number should remain unchangedelse:pass

Of course, in this case, one would probably use return instead of assignment, but in cases where mutation is desired, this works best. 当然,在这种情况下,可能会使用return而不是赋值,但是在需要突变的情况下,这种方法效果最佳。

The use of pass here is especially useful to warn future maintainers (including yourself!) not to put redundant steps outside of the conditional statements. 在这里使用pass对于警告将来的维护者(包括您自己!)不要在条件语句之外放置多余的步骤特别有用。 In the example above, flag is set in the two specifically mentioned cases, but not in the else -case. 在上面的例子中, flag被设置在这两个具体提及的情况下,但不是在else -案例。 Without using pass , a future programmer might move flag = True to outside the condition—thus setting flag in all cases. 如果不使用pass ,将来的程序员可能会将flag = True移到条件之外,因此在所有情况下都应设置flag


Another case is with the boilerplate function often seen at the bottom of a file: 另一种情况是通常在文件底部看到样板函数:

if __name__ == "__main__":pass

In some files, it might be nice to leave that there with pass to allow for easier editing later, and to make explicit that nothing is expected to happen when the file is run on its own. 在某些文件中,最好将其保留pass ,以便以后进行更轻松的编辑,并明确表明在单独运行文件时不会发生任何事情。


Finally, as mentioned in other answers, it can be useful to do nothing when an exception is caught: 最后,如其他答案中所述,捕获异常时不执行任何操作可能很有用:

try:n[i] = 0
except IndexError:pass

如何使用pass语句?相关推荐

  1. python的用途实例-python中pass语句意义与作用(实例分析)

    想必大家都是刚刚才开始接触python这门语言的,今天这篇文章就来带大家来了解一下python这门语言之中常常会用到的一个语句pass语句.想必大家都很好奇python中pass作用是什么,接下来我就 ...

  2. Python pass 语句

    概述 Python pass是空语句,是为了保持程序结构的完整性. pass 不做任何事情,一般用做占位语句. 语法 Python 语言 pass 语句语法格式如下: pass 栗子 #!/usr/b ...

  3. Python语言学习之常见语句命令那些事:python和常见语句命令(条件语句、pass语句)使用方法之详细攻略

    Python语言学习之常见语句命令那些事:python和常见语句命令(条件语句.pass语句)使用方法之详细攻略 目录 Python常见语句命令 1.python的条件语句 2.Python之pass ...

  4. python中的pass语句_Python中pass的作用与使用代码示例

    本篇文章小编给大家分享一下Python中pass的作用与使用代码示例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看. Python中pass的作用 空语 ...

  5. Python基础教程:Python pass语句详解

    2019独角兽企业重金招聘Python工程师标准>>> Python pass 语句 Python pass是空语句,是为了保持程序结构的完整性. pass 不做任何事情,一般用做占 ...

  6. python的pass语句_适用于pass语句的Python程序

    python的pass语句 Prerequisite: pass statement in Python 先决条件: Python中的pass语句 In the below program, we h ...

  7. python的pass语句_Python | 演示pass语句的示例

    python的pass语句 python中的pass语句 (pass statement in python) "pass" is a type of null operation ...

  8. python中pass的使用_Python中pass语句的作用

    Python中pass语句实际上一种不会产生任何操作的语句.主要用于创建空的代码块或空函数. 1.用于创建空的代码块 下面程序用于找出20以内所有能被3整除的整数. list1 = [] for i ...

  9. [转载] Python 中 pass 语句的作用是什么?

    参考链接: Python pass语句 在编写代码时只写框架思路,具体实现还未编写就可以用 pass 进行占位,使程序不报错,不会进行任何操作. def func(): pass

最新文章

  1. 2022-2028年中国粘网胶行业市场深度评估及发展前景规划报告
  2. TCP/IP协议之网络链接的背后故事
  3. 排序算法——单链表快速排序(划分函数从一边划分)
  4. 初识OSPF(三)——路由重分发及虚链路
  5. JS如何禁止别人查看网站源码
  6. django-cookie的认识和基本使用与值查看
  7. django的form常用字段和参数
  8. 理想汽车4月交付5539辆 累计交付51715辆
  9. 使用Webbrowser的一点心得体会
  10. Google出品,必属精品
  11. 电源模块的选择、国内外知名电源模块厂家排名及厂家优势特点汇总
  12. MATLAB调用ANSYS进行有限元分析
  13. 标准c++库、stl库,boost库,qt库
  14. 基于片内Flash的提示音播放程序
  15. html安卓修改器,让安卓面目全非:尖兵手机修改器
  16. gtp传输java_一种GTP数据包传输方法、相关装置及存储介质与流程
  17. LMS自适应波束形成算法(MATLAB)
  18. 鸿蒙1030鸿蒙,鸿蒙系统申请
  19. AFNetworking 之加载网络图片
  20. 兴文石海旅游策划方案——石来运转天下览!

热门文章

  1. 【转】同步的HttpClient使用详解
  2. SQL Server基础之索引
  3. 在MyEclipse中配置Tomcat服务器
  4. 【长篇连载】桌面管理演义 尾声
  5. 记录学习WeakReference发现的问题
  6. centos7部署posgresql和kong总结
  7. GitHub创建个人主页
  8. override和new的区别
  9. ORACLE常用函数汇总【转】
  10. genlist -s 192.168.21.\*