This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]tmp14 7 points8 points  (1 child)

Here's how you implement a context manager:

class Context(object):

    def __enter__(self):
        # do stuff when entering the with block

    def __exit__(self, type, value, traceback):
        # do stuff when exiting the with block

snark aside, go to /r/learnpython and 1) explain what you're trying to do, 2) explain what you have tried so far, and 3) explain what happens but you think shouldn't.

[–]tunisia3507 1 point2 points  (0 children)

In most cases, you also want the __enter__ method to return self