class Conn(object):

    def close(self):
        print('Closing')


class Device(object):

    def __init__(self, conn):
        self.conn = Conn()
with connection() as conn:
    pass
How to use contextmanagers for instance variables