- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
为什么要使用 contextlib.suppress
来抑制异常,而不是使用 try
/except
和 pass
?
这两种方法在字符数量上没有区别(如果有的话,suppress
有更多的字符),即使代码经常以 LOC 而非字符计入,suppress
似乎也比 try
/except
在两种情况下都慢得多,无论何时引发错误:
Python 3.5.0 (v3.5.0:374f501f4567, Sep 13 2015, 02:27:37) [MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> from timeit import timeit
>>> # With an error
>>> timeit("""with suppress(ValueError):
x = int('a')""", setup="from contextlib import suppress")
1.9571568971892543
>>> timeit("""try:
x = int('a')
except ValueError:
pass""")
1.0758466499161656
>>> # With no error
>>> timeit("""with suppress(ValueError):
x = int(3)""", setup="from contextlib import suppress")
0.7513525708063895
>>> timeit("""try:
x = int(3)
except ValueError:
pass""")
0.10141028937128027
>>>
最佳答案
它在不牺牲可读性的情况下减少了两行代码。
对于嵌套或连续的代码块可能特别方便。比较:
try:
a()
try:
b()
except B:
pass
except A:
pass
对比:
with suppress(A):
a()
with suppress(B):
b()
它还允许表达意图:
with suppress(SpecificError): do_something()
说如果在做某事时引发错误,不要传播该错误try: do_something() except SpecificError: pass
说做某事,如果出现错误,不要传播错误它不太重要,因为大多数人不会注意到差异。
关于python - 为什么使用 contextlib.suppress 而不是 try/except 和 pass?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34566806/
自从我知道了这个模式,我就一直在使用 with open('myfile.txt','w') as myfile: with contextlib.redirect_stdout(myfile
我在 Ubuntu 上使用 Python3.5。以下脚本创建一个文件 out 并用越来越多的“Hello”行填充它: import contextlib with contextlib.redirec
我正在尝试创建一个包装器来使上下文对象成为可选的。当条件为真时,事物应该表现得像包装的上下文对象,否则它应该表现得像无操作上下文对象。此示例适用于使用一次包装对象,但失败了,它被重新使用。 例子: i
我明白了why contextlib.nested is deprecated . 但是如果我为旧的 python 版本编写程序而没有 with 的多种形式(即 < 2.7),我(几乎)别无选择。 为
有谁知道我在哪里可以找到这个 python 模块“contextlib”? root@overo:~# python
我们的代码根据运行时参数调用可变数量的上下文管理器: from contextlib import nested, contextmanager @contextmanager def my_cont
为什么我可以跳过在 yield log_func 中调用 log_func,即我可以只写 yield 而代码仍然有效。这是为什么? yield 在这种情况下如何工作? import time from
假设我有一个上下文管理器: @contextmanager def cm(x): y = f(x) z = yield y g(z) 如何将 z 发送到上下文管理器? 我试过:
我使用 Python2.7 并且我想要函数:contextlib.redirect_stdout . 我的意思是,我想重定向特定函数的输出(不是所有程序)。 问题是 - 只有 Python3 支持“c
作为将我的游戏引擎代码转换为 cython 的一部分,我正在移植我的顶点缓冲区对象 (Vbo) 类。我使用这个 Vbo 类将 3D 模型数据发送到 GPU。代码 (vbo.pyx) 当前如下所示: c
我在 python 中使用 with 语句( PEP 343 ) 时遇到了一些问题,以便在上下文之后自动管理资源清理。特别是,with 语句 始终假定资源清理方法是 .close()。 IE。在下面的
为了以编程方式组合上下文管理器,我使用以下代码: == helpers.py == from contextlib import nested import mock def multiple_pat
context.nested 已被弃用,我试图理解这背后的基本原理,在阅读了很长时间的文档后,我看不到一个例子来说明第二个问题: Secondly, if the __enter__() method
我真的很喜欢“with open('in_file'') as f”这个语法。我想将该语法用于必须打开和关闭的我自己的资源。 但是,我不明白如何更改我的 open() 方法以启用“with”语法。我可
from contextlib import closing def init_db(): with closing(connect_db()) as db: with app
尝试从 docker 运行命令时,我遇到了这个错误 google 计算引擎上的 docker 容器。 这是堆栈跟踪: Traceback (most recent call last): File
为了以编程方式组合上下文管理器,我使用以下代码: == helpers.py == from contextlib import nested import mock def multiple_pat
为了说明我的问题,这里有一个不使用 contextlib 的基本请求: import urllib.request url = "http://www.example.com/"
在寻找性能错误的过程中,我终于确定问题的根源是 contextlib 包装器。开销相当惊人,我没想到这是减速的根源。减速在 50 倍的范围内,我无法承受循环。如果它有可能显着减慢速度,我肯定会感谢文档
为什么要使用 contextlib.suppress 来抑制异常,而不是使用 try/except 和 pass? 这两种方法在字符数量上没有区别(如果有的话,suppress 有更多的字符),即使代
我是一名优秀的程序员,十分优秀!