python with语句里有return, exit还会执行吗

测试代码:

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语句执行之后。

你可能感兴趣的:(python with语句里有return, exit还会执行吗)