任何韵或理由为什么一个可赎回代码在Python 3.5 with
声明中不可调用?为什么不能在一个Python中调用可调用的块?
class Seriously(object):
def __init__(self, name):
self.name = name
def __enter__(self):
print("Enter " + self.name)
def __call__(self):
print("Call " + self.name)
def __exit__(self, type, value, traceback):
print("Exit " + self.name)
a = Seriously('a')
a.__enter__()
a()
a.__enter__()
with Seriously('b') as b:
b()
严重产生
Enter a
Call a
Enter a
Enter b
Exit b
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-6-91a5d11e1b2e> in <module>()
18
19 with Seriously('b') as b:
---> 20 b()
TypeError: 'NoneType' object is not callable
什么我在PEP 343失踪?
在__call__结束处'return self'做到了。会接受一次,谢谢。 –