def closeall(cls):
       "Recursively close all instances of cls and its subclasses"
       print 'Closing instances of %s' % cls # you may remove this print
       for obj in reversed(cls._open_instances):
           obj.close()
           cls._open_instances.remove(obj)
       for subc in cls.__subclasses__():
           subc.closeall()
Recursively close all instances of cls and its subclasses
recipes/Python/523007_Semiautomatic_resource_management/recipe-523007.py