我应该如何从 with 语句返回有趣的值?
有没有比使用全局变量从上下文管理器获取有趣值更好的方法?
@contextmanager
def transaction():
global successCount
global errorCount
try:
yield
except:
storage.store.rollback()
errorCount += 1
else:
storage.store.commit()
successCount += 1
其他可能性:
单例
某种全局变量...
元组作为上下文管理器的参数
使函数更具体地针对问题/更少可重用
将特定属性作为参数保存的实例上下文管理器
与元组有相同的问题,但更清晰
在上下文末尾引发异常持有价值观的经理。
真是个坏主意
Is there a better way than using globals to get interesting values from a context manager?
@contextmanager
def transaction():
global successCount
global errorCount
try:
yield
except:
storage.store.rollback()
errorCount += 1
else:
storage.store.commit()
successCount += 1
Other possibilities:
singletons
sort of globals...
tuple as an argument to the context manager
makes the function more specific to a problem /less reusable
instance that holds the specific attributes as an argument to the context manager
same problems as the tuple, but more legible
raise an exception at the end of the context manager holding the values.
really bad idea
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请参阅http://docs.python.org/reference/datamodel.html#context- manager
创建一个类,用于保存成功和错误计数,并实现 __enter__ 和 __exit__ 方法。
See http://docs.python.org/reference/datamodel.html#context-managers
Create a class which holds the success and error counts, and which implements the
__enter__
and__exit__
methods.我仍然认为您应该创建一个类来保存错误/成功计数,正如我在您中所说的 最后一个问题。 我猜你有自己的类,所以只需向其中添加类似的内容:(
如果调用
contextmanager
后没有异常,则type
为 None),然后你可能已经在某处使用它,它将调用
contextmanager
并运行您的__exit__()
代码。 编辑:正如 Eli 评论的那样,仅当您想要重置计数器时才创建新的事务实例。I still think you should be creating a class to hold you error/success counts, as I said in you last question. I'm guessing you have your own class, so just add something like this to it:
(
type
is None if there are no exceptions once invoking thecontextmanager
)And then you probably are already using this somewhere, which will invoke the
contextmanager
and run your__exit__()
code. Edit: As Eli commented, only create a new transaction instance when you want to reset the coutners.“元组作为上下文管理器的参数
使函数更具体地解决问题/更少可重用”
错误。
这使得上下文管理器保留状态。
如果您不实现除此之外的任何内容,它将是可重用的。
但是,您实际上无法使用元组,因为它是不可变的。 你需要一些可变的集合。 我想到了字典和类定义。
因此,推荐的实现是
“将特定属性作为上下文管理器参数的实例”。
您只需要一个具有两个属性的简单类定义。 但是,您的事务状态是有状态的,您需要在某处保留状态。
"tuple as an argument to the context manager
makes the function more specific to a problem /less reusable"
False.
This makes the context manager retain state.
If you don't implement anything more than this, it will be reusable.
However, you can't actually use a tuple because it's immutable. You need some mutable collection. Dictionaries and class definitions come to mind.
Consequently, the recommended implementation is
"instance that holds the specific attributes as an argument to the context manager"
A simple class definition with two attributes is all you need. However, your transaction status is stateful and you need to retain state somewhere.