我正在努力学习python,结果我发现
with..as
构造,它的用法如下:
with open("somefile.txt", 'rt') as file:
print(file.read())
# at the end of execution file.close() is called automatically.
因此,作为一种学习策略,我尝试执行以下操作:
class Derived():
def __enter__(self):
print('__enter__')
def __exit__(self, exc_type, exc_value, traceback):
print('__exit__')
with Derived() as derived:
print(derived)
我得到了下面的输出:
__enter__
None
__exit__
我的问题是:
print(derived)
返回None
对象而不是Derived
对象?https://stackoverflow.com/questions/23313535
复制相似问题