Created
August 18, 2017 12:55
-
-
Save 92hackers/d87bd46a116762325e5706e07bc56281 to your computer and use it in GitHub Desktop.
Wrap open in a context manager, you must implement __enter__ and __exit__ methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class File(object): | |
def __init__(self, file_name, method): | |
self.file_obj = open(file_name, method) | |
def __enter__(self): | |
return self.file_obj | |
def __exit__(self, type, value, traceback): | |
self.file_obj.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment