python - with statement work on class -
{class foo(object): def __enter__ (self): print("enter") def __exit__(self,type,value,traceback): print("exit") def method(self): print("method") foo() instant: instant.method()} execute py file , console shows these message:
enter exit instant.method() attributeerror: 'nonetype' object has no attribute 'method' unable find methods?
__enter__ should return self:
class foo(object): def __enter__ (self): print("enter") return self def __exit__(self,type,value,traceback): print("exit") def method(self): print("method") foo() instant: instant.method() yields
enter method exit if __enter__ not return self, returns none default. thus, instant assigned value none. why error message
'nonetype' object has no attribute 'method'
(my emphasis)
Comments
Post a Comment