> On 18 Nov 2019, at 19:27, Brendan Barnwell <brenb...@brenbarn.net> wrote: > > On 2019-11-18 10:49, Random832 wrote: >> I would say open() is arguably a wart. Had context managers existed >> in the language all along, it should not be possible to do anything >> that creates an open-ended future requirement to do something (i.e. >> "require that the returned object gets closed at some stage") without >> either using a with statement or writing code to manually call >> __enter__ and __exit__. The likely most common case being a class >> that holds an open file, which should really have its own context >> manager that forwards to the file's. > > I agree. I think this discussion wouldn't be necessary if there were a > context manager that was like open() but opened the file in __enter__ and > closed it in __exit__. I would go so far as to say I think it would be a > good idea to add such a context manager to Python and deprecate open(). > > When I teach Python, I don't really even explain open() in isolation to > students, I just tell them to always use it with a with statement. If I see > code that uses open() outside a with statement it's a red flag to me. These > hypothetical cases where you might want to use open() outside a with > statement seem, to me, to be fairly esoteric. The best policy is for every > file-opening operation to occur as part of a context manager,
Esoteric? I work with files that are open beyond the scope of a single block of code all the time. Clearly I need to arrange to close the file eventually. Why would I want the uglyness of __exit__ rather then close()? I use with all the time if the open() is with linear code. Barry > > -- > Brendan Barnwell > "Do not follow where the path may lead. Go, instead, where there is no > path, and leave a trail." > --author unknown > _______________________________________________ > Python-ideas mailing list -- python-ideas@python.org > To unsubscribe send an email to python-ideas-le...@python.org > https://mail.python.org/mailman3/lists/python-ideas.python.org/ > Message archived at > https://mail.python.org/archives/list/python-ideas@python.org/message/STCWSHQJWBDNZILIJPPW7HQJTGVMH6GJ/ > Code of Conduct: http://python.org/psf/codeofconduct/ > _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/IAVAIUBF73T5ON7N5JMU7DODGEJK4LP4/ Code of Conduct: http://python.org/psf/codeofconduct/