测试代码:
class T:
def __enter__(self):
print('enter')
class T:
def __enter__(self):
print('enter')
def __exit__(self, exc_type, exc_val, exc_tb):
print('exit')
def t():
with T():
print('sth')
return print('do sth')
> t()
enter
sth
do sth
exit
结论:会执行,且是在return语句执行之后。