classification
Title: Add fileobj property to csv reader and writer objects
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: samwyse, serhiy.storchaka
Priority: normal Keywords:

Created on 2018-03-27 16:17 by samwyse, last changed 2018-03-27 17:17 by serhiy.storchaka.

Messages (2)
msg314538 - (view) Author: Samwyse (samwyse) Date: 2018-03-27 16:17
Many objects have properties that allow access to the arguments used to create them.  In particular, file objects have a name property that returns the name used when opening a file.  A fileobj property would be convenient, as you otherwise, for example, need to pass an extra argument to routines that need both the csv object and the underlying file object.  Adopting this enhancement would also provide consistency with the dialect constructer argument, which is available as an object property.

Changing the fileobj while the csv object is in use would open a can of worms, so this should be a read-only property.

Optionally, the fileobj property could be reflected in the DictReader and DictWriter classes, but the value would be accessible via the .reader and .writer properties of those classes.
msg314541 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-03-27 17:17
> A fileobj property would be convenient, as you otherwise, for example, need to pass an extra argument to routines that need both the csv object and the underlying file object.

But you should already have this object for creating csv.reader() or csv.writer().

Even if keep the argument of the constructor as an instance attribute, there is a problem with naming this attribute. csv.reader works not only with files. It accepts arbitrary iterable.

>>> it = iter(['Spam, Spam, Spam, Spam, Spam, Baked Beans', 'Spam, Lovely Spam, Wonderful Spam'])
>>> list(csv.reader(it))
[['Spam', ' Spam', ' Spam', ' Spam', ' Spam', ' Baked Beans'], ['Spam', ' Lovely Spam', ' Wonderful Spam']]

The dialect property is not a copy of the dialect constructer argument.
History
Date User Action Args
2018-03-27 17:17:55serhiy.storchakasetnosy: + serhiy.storchaka

messages: + msg314541
versions: - Python 2.7
2018-03-27 16:17:09samwysecreate