{class foo(object):
def __enter__ (self):
print("Enter")
def __exit__(self,type,value,traceback):
print("Exit")
def method(self):
print("Method")
with foo() as instant:
instant.method()}
Run Code Online (Sandbox Code Playgroud)
执行此py文件,控制台显示以下消息:
Enter
Exit
instant.method()
AttributeError: 'NoneType' object has no attribute 'method'
Run Code Online (Sandbox Code Playgroud)
无法找到方法?