[Tutor] line number when reading files using csv module

Kent Johnson kent37 at tds.net
Mon Oct 30 14:21:56 CET 2006


Duncan Gibson wrote:
> Duncan Gibson wrote:
>>>     import csv
>>>
>>>     class MyReader(object):
>>>         
>>>         def __init__(self, inputFile):
>>>             self.reader = csv.reader(inputFile, delimiter=' ')
>>>             self.lineNumber = 0
>>>
>>>         def __iter__(self):
>>>             self.lineNumber += 1
>>>             return self.reader.__iter__()
>>>
>>> Is there some other __special__ method that I need to forward to the
>>> csv.reader, or have I lost all control once __iter__ has done its job?
> 
> Kent Johnson wrote:
>> __iter__() should return self, not self.reader.__iter__(), otherwise 
>> Python is using the actual csv.reader not your wrapper. And don't 
>> increment line number here.
>>
>> You lost control because you gave it away.
> 
> 
> Thanks Kent. The penny has dropped and it makes a lot more sense now.
> 
> I was looking for at __iter__ as a special function that *created* an
> iterator, but all it really does is signal that the returned object
> will implement the iterator interface, and the next() method in
> particular. 

I think you might still be a tiny bit confused. __iter__() is not just a 
marker, it does return an iterator. It helps if you distinguish two 
cases - iterable and iterator.

An iterable object is one for which iter(obj) returns an iterator. An 
iterator is an object with a next() method. As a special case, iterators 
are also iterable - if obj is already an iterator, then iter(obj) is 
just obj.

In modern Python (since 2.2) iter() is implemented by calling the 
special method __iter__() which should return an iterator. For a 
sequence like a list, __iter__() will return a new iterator object. 
Again, as a special case, __iter__() for an iterator returns the 
iterator itself.

You might want to read this:
http://www.python.org/doc/2.2.3/whatsnew/node4.html

I hope I am shedding light here :-)
Kent



More information about the Tutor mailing list