Object becomes None when using a context manager

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).

Leave a Comment