python – 在体内代码之前调用的上下文管理器出口
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 在体内代码之前调用的上下文管理器出口,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1728字,纯文字阅读大概需要3分钟。
内容图文
我一直在玩Python自己的上下文管理器.我看到一些奇怪的行为很可能是由于我的实施.
我看到在’with’上下文中的语句之前调用的__exit__代码.例如,以下是代码段:
with ProgressBar(10) as p:
p.update(1)
这是例外:
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
AttributeError: 'NoneType' object has no attribute 'update'
我把调试放在所有的__enter __,__ exit__,并更新了我的上下文管理器的方法.看起来在update()之前调用__exit__.这没有任何意义所以我必须错过一些简单的东西.
这是我的简单上下文管理器类:
class ProgressBar(object):
"""Progress bar that normalizes progress to [0 - 100] scale"""
def __init__(self, max_value):
"""Create progress bar with max_value"""
self._current_value = 0.0
self._completed_value = 100.0
self._max_value = float(max_value)
print 'init', max_value
def __enter__(self):
"""Start of context manager, 'with' statement"""
print 'enter'
self._current_value = 0.0
def __exit__(self, exc_type, exc_value, traceback):
"""Start of context manager, 'with' statement"""
print 'exit'
self._current_value = self._completed_value
# Not handling any exceptions, so they'll be raised automatically
# To ignore exceptions return True or inspect arguments to handle
return False
def update(self, value):
"""Update progress value"""
print 'update'
if value >= self._max_value:
self._current_value = 100
else:
self._current_value = (value / self._max_value) * self._completed_value
print '\r%s' % (self._current_value),
解决方法:
从文档:
object.__enter__(self)
Enter the runtime context related to this object. The
with
statement
will bind this method’s return value to the target(s) specified in
theas
clause of the statement, if any.
你没有从__enter__返回任何东西(因此你一如既往地返回None).如果你回归自我,你会得到
init 10
enter
update
10.0 exit
内容总结
以上是互联网集市为您收集整理的python – 在体内代码之前调用的上下文管理器出口全部内容,希望文章能够帮你解决python – 在体内代码之前调用的上下文管理器出口所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。