{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()}
Execute this py file and console shows these message:
Enter
Exit
instant.method()
AttributeError: 'NoneType' object has no attribute 'method'
unable to find methods?
The problem is that your
__enter__
method does not returnself
.__enter__
should returnself
:yields
If
__enter__
does not returnself
, then it returnsNone
by default. Thus,instant
is assigned the valueNone
. This is why you get the error message(my emphasis)