Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 理解上下文管理器类中返回自我的目的_Python_Python 2.7_Class_With Statement_Contextmanager - Fatal编程技术网

Python 理解上下文管理器类中返回自我的目的

Python 理解上下文管理器类中返回自我的目的,python,python-2.7,class,with-statement,contextmanager,Python,Python 2.7,Class,With Statement,Contextmanager,试图了解上下文管理器是如何捕获错误的,但更具体地说,\uuuu enter\uuuu()方法在创建用作上下文管理器的类中的作用,它在这里的“错误捕获”过程中是如何工作的,以及为什么\uuu enter\uuuuu()方法中只返回了self 给定以下使用上下文管理器捕捉错误的情况: import unittest class InvoiceCalculatorTests(unittest.TestCase): def test_no_pay(self): with self

试图了解上下文管理器是如何捕获错误的,但更具体地说,
\uuuu enter\uuuu()
方法在创建用作上下文管理器的类中的作用,它在这里的“错误捕获”过程中是如何工作的,以及为什么
\uuu enter\uuuuu()
方法中只返回了
self

给定以下使用上下文管理器捕捉错误的情况:

import unittest
class InvoiceCalculatorTests(unittest.TestCase):
    def test_no_pay(self):
        with self.assertRaises(ValueError):
            pay = divide_pay(0, {"Alice": 3.0, "Bob": 3.0, "Carol": 6.0})
以下是我认为的
资产的

class _AssertRaisesContext(_AssertRaisesBaseContext):
    """A context manager used to implement TestCase.assertRaises* methods."""

    _base_type = BaseException
    _base_type_str = 'an exception type or tuple of exception types'

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, tb):
        if exc_type is None:
            try:
                exc_name = self.expected.__name__
            except AttributeError:
                exc_name = str(self.expected)
            if self.obj_name:
                self._raiseFailure("{} not raised by {}".format(exc_name,
                                                                self.obj_name))
            else:
                self._raiseFailure("{} not raised".format(exc_name))
        else:
            traceback.clear_frames(tb)
        if not issubclass(exc_type, self.expected):
            # let unexpected exceptions pass through
            return False
        # store exception, without traceback, for later retrieval
        self.exception = exc_value.with_traceback(None)
        if self.expected_regex is None:
            return True

        expected_regex = self.expected_regex
        if not expected_regex.search(str(exc_value)):
            self._raiseFailure('"{}" does not match "{}"'.format(
                     expected_regex.pattern, str(exc_value)))
        return True
我试着去了解一些情况,但要理解其中所包含的内容有点超出了我目前的知识/理解。有人能用相对外行的术语解释一下,
\uuuuuu enter\uuuuu()
\uuuuuuu exit\uuuuuuuuuuu()
在“捕获”这里的值错误的过程中所起的作用,以及为什么
\uuuuuuuuuuu enter\uuuuuuuuuuuu()
只是返回自我

\uuuu enter\uuuu()
用于设置,此特定的上下文管理器不需要任何设置。因此,它所要做的就是返回要在
as
子句中指定的对象,它本身就是