You will need to return selffrom __enter__:
def __enter__(self):
print "enter"
return self
Your statement is withactually equivalent to:
a = A(10).__enter__()
try:
print a.i
except:
a.__exit__(exception and type)
raise
else:
a.__exit__(None, None, None)
So, you need to return something, otherwise it awill have a value None(default return value), but Nonedoes not have an attribute with a name i, so you will get AttributeError.
source
share