- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我正在尝试创建一个装饰器类来计算函数被调用的次数,但我收到一条错误消息:
"TypeError: __exit__() takes exactly 1 argument (4 given)"
我真的不知道我是如何给它四个参数的。我的代码如下所示:
class fcount2(object):
__instances = {}
def __init__(self, f):
self.__f = f
self.__numcalls = 0
fcount2.__instances[f] = self
def __call__(self, *args, **kwargs):
self.__numcalls += 1
return self.__f(*args, **kwargs)
def __enter__(self):
return self
def __exit__(self):
return self
@staticmethod
def count(f):
return fcount2.__instances[self.__f].__numcalls
@fcount2
def f(n):
return n+2
for n in range(5):
print f(n)
print 'f count =',f.count
def foo(n):
return n*n
with fcount2(foo) as g:
print g(1)
print g(2)
print 'g count =',g.count
print 'f count =',f.count
with fcount2(f) as g:
print g(1)
print g(2)
print 'g count =',g.count
print 'f count =',f.count
with f:
print f(1)
print g(2)
print 'g count =',g.count
print 'f count =',f.count
还有其他一些我应该(或不应该)传递给 def exit 函数的参数吗?任何提示或想法将不胜感激。
顺便说一句,我的代码行“print 'f count =',f.count”似乎是在输出内存地址而不是值,但这是一个完全不同的问题。
最佳答案
__exit__()
方法应该接受有关 with:
block 中出现的异常的信息。见 here .
对您的代码进行以下修改:
def __exit__(self, exc_type, exc_value, tb):
if exc_type is not None:
traceback.print_exception(exc_type, exc_value, tb)
# return False # uncomment to pass exception through
return True
然后您可以尝试在您的 with:
block 之一中引发异常,它将被 __exit__()
捕获。
关于python - __enter__ 和 __exit__ 如何在 Python 装饰器类中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22417323/
是否有一种 Pythonic 的方式来自动 __exit__ 一个类的所有成员? class C: def __init__(self): self.a = open('foo
我明白了 __enter__ 和__exit__ 用于实现上下文管理器。 如果在 with 语句中发生异常,异常的类型、值和回溯将传递给 __exit__ 方法。 __exit__ 可以处理异常: 返
我尝试从以下位置运行示例:https://docs.python.org/3/library/socketserver.html#socketserver-tcpserver-example 在我的笔
我无法在测试中将 __exit__ 方法附加到 mockito 模拟,以便模拟在 with 语句中使用它时不会介意。 这是一个不断引发 AttributeError: __exit__: 的测试示例
我在提醒自己“关键字”时偶然发现了这篇文章 http://effbot.org/zone/python-with-statement.htm 。我只是有一个小问题,可能是显而易见的问题。文章指出,这“
我已经通过.csv文件来发布请求, input_file = data.get('file', None) with input_file as datasheet: header =
调查 documentation python 中的 with 语句: The context manager’s __exit__() method is invoked. 我想知道如果我这样做会发
考虑到这个小的 Python 类,每当我使用 Ctrl+C 停止脚本时,__exit__ 函数都会在引发异常之前运行: import time class MyClass(object): d
一些背景:我在一家大银行工作,我试图重新使用一些 Python 模块,我无法更改,只能导入。我也没有安装任何新实用程序/功能等的选项(在 Linux 上运行 Python 2.6)。 我目前有这个:
我正在制作类似任务调度程序的东西,使用生成器作为协程。在下面的代码中,我需要确定性地执行 print cleanup。 从我的交互看来,将对象释放给垃圾收集器会导致上下文管理器退出。但是,我知道最好不
我知道从上下文管理器的 __exit__() 方法中重新引发异常是不好的风格。因此,我想在实例上附加一个属性,该属性可以携带上下文信息,如果我让异常通过或捕获它,则该信息不可用。这将避免重新提高它。
考虑以下 Python 2.x 代码片段。 from __future__ import print_function class myfile(file): def __exit__(sel
使用 pool.map(funct, iterable) 时出现此错误: AttributeError: __exit__ 没有解释,只是堆栈跟踪到模块内的 pool.py 文件。 这样使用: wit
我最近想知道当 __enter__ 引发异常时不隐式调用 __exit__ 的原因是什么?为什么要这样设计?我正在实现服务运行器类以供 'with' 关键字使用,结果 __exit__ 从未被调用。
鉴于以下 ContextManager类,这里是 __exit__ 的顺序以下示例中的调用 - 声明的相反顺序。 有趣的是,__exit__可以多次调用。这在 2.7 的所有 Python 版本中都是
我是单元测试的新手,我正在尝试找到一种方法来测试 with 关键字是否在我的对象中正常工作。 在这种情况下,我的对象有一个创建临时目录的 __enter__ 方法和应该销毁它的 __exit__ 方法
类有一个可定义的函数__exit__,允许实现上下文管理器。 它接受必需的参数: def __exit__(self, exc_type, exc_val, exc_tb): 但我找不到关于这些参数是
如果它引发异常(可能多次,可能有延迟),我想在 exit() 方法中再次调用代码对象。我知道使用装饰器很容易做到,但我的动机是有时我只想重复一些我不想提取到单独函数并装饰它的代码片段。我正在寻找这些方
当使用不带 as 的 with 语句时,__enter__ 函数永远不会执行,但 __exit__方法会? 例子: with test: test.do_something test.__exit
我正在尝试使用 Mock 修补一些上下文管理器功能,以便我可以测试代码在给定好的、坏的和垃圾输入的情况下是否能做出明智的事情。这是带有 with 语句的测试代码。补丁已在我的代码中的正确位置完成。 @
我是一名优秀的程序员,十分优秀!