我正在尝试学习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
个对象?