3

我一直在尝试用 Python 制作自己的上下文管理器。我看到一些奇怪的行为很可能是由于我的实施。

我看到__exit__在“with”上下文中的语句之前调用的代码。例如,这里是代码片段:

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__在 update() 之前调用的。这没有任何意义,所以我必须遗漏一些简单的东西。

这是我的简单上下文管理器类:

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),
4

2 回答 2

9

从文档中:

object.__enter__(self)

输入与此对象相关的运行时上下文。该with语句将将此方法的返回值绑定到该as语句子句中指定的目标(如果有)。

你没有返回任何东西__enter__(因此你一如既往地返回 None )。如果你return self,你会得到

init 10
enter
update
10.0 exit
于 2012-09-08T17:28:02.580 回答
2

with语句将上下文管理器方法的返回值绑定到变量。在您的情况下,这意味着绑定到(您根本没有 return 语句,默认值也是如此),引发 an 并调用该方法。__enter__pNone__enter__NoneAttributeError__exit__

解决方案是self__enter__方法返回:

def __enter__(self):
    """Start of context manager, 'with' statement"""

    print 'enter'
    self._current_value = 0.0
    return self
于 2012-09-08T17:28:50.077 回答