How to overide "for line in file:"

David David
Sun Feb 22 01:13:32 CET 2004


How does one overide the iterator implied by the construct "for line
in file:"?

For example, suppose I have a file containing row,col pairs on each
line, and I wish to write a subclass of file that will transform these
to x,y coordinates using an affine transform. I'd like it to look
something like this (but this clearly doesn't work):

class myFile(file):
    def __init__(self,name,affine):
        self.affine = affine
        file.__init__(name)

    def next(self):
        for line in file.__iter__(self):
            r,c = ",".split(line[:-1]
            yield self.affine(r,c)


Thanks in advance!
dave



More information about the Python-list mailing list