python 中关于 with 语句的使用
with ... as var:
pass
- 任何对象,只要正确实现了上下文管理(通过__enter__和__exit__这两个方法实现),就可以用于with语句。
def __enter__(self):
print('Begin')
return self
python 中关于 with 语句的使用
with ... as var:
pass
def __enter__(self):
print('Begin')
return self