标签:
# customized object
class VOW(object):
def __init__(self, text):
self.text = text
def __enter__(self):
self.text = "I say: " + self.text # add prefix
return self # note: return an object
def __exit__(self,exc_type,exc_value,traceback):
self.text = self.text + "!" # add suffix
with VOW("I‘m fine") as myvow:
print(myvow.text)
print(myvow.text)
with VOW("I‘m fine") as myvow: 该处的myvow对应 __enter__ 中的return self
标签:
原文地址:http://www.cnblogs.com/Citizen/p/4563502.html