What's the Pythonic way to write an auto-closing class?
python
Solution
What you're doing looks totally fine and Pythonic. Although, the `contextlib` standard library already has something similar, but you'll have to rename your `Close` methods to `close`.
import contextlib
with contextlib.closing(thing):
print thing
I would recommend using this instead. After all, the recommended naming convention for Python methods is `all_lowercase_with_underscores`.
Problem
I'm a noob with Python, but I've written an auto-close function like this.. ``` @contextmanager def AutoClose(obj): try: yield obj finally: obj.Close() ``` I have three classes that have a Close() method that this function can be used with. Is this the most Pythonic solution? Should I be doing something in the classes themselves instead?