On Fri, 15 Nov 2019 at 12:04, Serhiy Storchaka <storch...@gmail.com> wrote: > > 15.11.19 12:40, Jonathan Fine пише: > > The original poster wanted, inside 'with' context management, to open > > several files. Context management is, roughly speaking, deferred > > execution wrapped in a try ... except .... statement. > > In case of open() there is no deferred execution. The resource is > acquired in open(), not in __enter__().
I've often thought that this was the root of various awkwardnesses with context managers. Ideally a well-behaved context manager would only use __exit__ to clean up after __enter__ but open doesn't do that. The nested context manager was designed for this type of well-behaved context manager but was then considered harmful because open (one of the most common context managers) misbehaves. Maybe some of these things could be simpler if it was clarified that a context manager shouldn't acquire resource before __enter__ and a new version of open was provided. -- Oscar _______________________________________________ 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/PV7SX6Q5FLSFQ2BFAW45IQWAOAXPMTNG/ Code of Conduct: http://python.org/psf/codeofconduct/