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/