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.v
Change the definition of X to
class X(object):
var1 = 1
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
pass
with assigns the return value of the __enter__() method to the name after as. Your __enter__() returned None, which was assigned to z.
I also changed the class to a new-style class (which is not critical to make it work).