Why doesn`t this work:
class X:
var1 = 1
def __enter__(self): pass
def __exit__(self, type, value, traceback): pass
with X() as z:
print z.var1
I get:
print z.var1
AttributeError: 'NoneType' object has no attribute 'var1'
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Change the definition of
Xtowithassigns the return value of the__enter__()method to the name afteras. Your__enter__()returnedNone, which was assigned toz.I also changed the class to a new-style class (which is not critical to make it work).