-
Notifications
You must be signed in to change notification settings - Fork 247
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
IO classes as context managers #175
Comments
+1, this would be great. |
It sounds good. |
Sure, we can do it in the next round of IO changes. But it will change the way IOs are used: Currently IOs open and close files automatically. So existing code would not work anymore because it doesn't call open() and it would not clean up because it doesn't call close(). |
That would be necessary anyway if we are going to support iteration as in #177. |
True. I think there is consensus since we are changing the API anyway. We can include it in the next round of IO changes. There are only two IOs left for this round, so we could start with that soon. |
Can this be implemented in the |
I think yes. |
For those who don't know, context managers are classes that can be used with python
with
statements. I think it would be beneficial for IO classes to be able to be used as context managers. This would provide a safe way to handle cleanup operations after running.To create a context manager, you need two methods,
__enter__
and__exit__
. For us,__enter__
would probably open the file, while__exit__
would close it.So I suggest that at the
BaseIO
level we implement abstractopen
andclose
methods that subclasses could re-implement to handle any cleanup. ThenBaseIO
would implement__enter__
and__exit__
methods that just callopen
andclose
, respectively.So someone could just do this:
People wouldn't need to know or care about specific cleanup operations they need to do with particular IO classes.
The text was updated successfully, but these errors were encountered: