How would I use a Contextmanager for instance variables? E.g. Let's assume I've got some Connection class, that must be closed on destruction. If I were to implement it as a ContextManager I could do.

with Connection() as c:
    c.write('FOO')
    c.ask('BAR?')

and it would get automatically closed on destruction. But what if I wanted to use it in a __init__ of another class, e.g. like the following example?

class Device(object):
    def __init__(self):
        self.connection = Connection()  # Must be closed on destruction.

I dont want it to be closed on exit of the constructor, it should die when the object get's destroyed. I could use __del__ but this has it's downsides. Being used to RAII in C++ it baffles me.

So what is the best way to do it in this case?

Edit
Report