I think the point here is not the context manager, but rather, having the reader open the file itself, rather than taking an already open file-like object.
And if it’s going to do that, it should provide. Context manager. Personally, while we are at it, I’d like to see a “read all” method, analogous to file.readlines(). For scripting use cases, reading a standard file format should be a one liner. -CHB On Sun, Sep 5, 2021 at 5:31 PM Oscar Benjamin <oscar.j.benja...@gmail.com> wrote: > On Mon, 6 Sept 2021 at 01:13, Greg Ewing <greg.ew...@canterbury.ac.nz> > wrote: > > > > On 6/09/21 3:07 am, C. Titus Brown via Python-ideas wrote: > > > with csv.DictReader.open(filename) as r: > > > for row in r: > > > … > > > > You can do this now: > > > > from contextlib import closing > > with closing(csv.DictReader.open(filename)) as r: > > ... > > What version of Python are you using? > > >>> import csv > >>> csv.DictReader.open > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > AttributeError: type object 'DictReader' has no attribute 'open' > > > IMO this is preferable than going around adding context manager > > methods to everything that has open-like functionality. > > I disagree. It would be better if resource acquisition (e.g. opening a > file) always took place in an __enter__ method so that it could always > be under control of a with statement. Having closing as a separate > function negates that because there has to be a separate function that > acquires the resource before the closing function is called and hence > before __enter__ is called. > > -- > 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/AR6IHZ2HY4TKZXKANUGA7HTPDQMBCLRC/ > Code of Conduct: http://python.org/psf/codeofconduct/ > -- Christopher Barker, PhD (Chris) Python Language Consulting - Teaching - Scientific Software Development - Desktop GUI and Web Development - wxPython, numpy, scipy, Cython
_______________________________________________ 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/GBWWTGTSWJAFJ64PR377NOCGYMHBOJUQ/ Code of Conduct: http://python.org/psf/codeofconduct/