标签:text close rac 调用 pre 上下 一个 val 代码
#with上下文管理器class SayHello(object):
def __init__(self, text):
self.text = text
def __enter__(self):
return self #必须返回对象
def __exit__(self, exc_type, exc_value, traceback):
self.text = "Bye!"
with SayHello("Hello") as greet:
print(greet.text)
print(greet.text)
输出:
Hello
Bye!
标签:text close rac 调用 pre 上下 一个 val 代码
原文地址:https://blog.51cto.com/13560219/2448681