Kent Johnson wrote: > If you just want to keep track of line numbers as you read the file by lines, > you could use enumerate(): > > f = open('myfile.txt') > for line_number, line in enumerate(f): > ...
This is neat, but not all of the parsers work on a 'line by line' basis, so sometimes there are additional calls to f.readline() or equivalent in other places in the code based on what has just been read. > What problem did you have when deriving from file? To be perfectly honest, I didn't try it because even if I had declared class MyFile(file): etc I couldn't see how to have an instance of MyFile returned from the built-in 'open' function. I thought this was the crux of the problem. So what I have done is provide a different interface completely, class MyFile(object): etc I could extend this to take the file name in the constructor, and add a MyFile.open() method, but then I can no longer substitute any MyFile instances in places that expect 'file' instances. Now that I've started to explain all of this to someone else, I'm starting to wonder whether it really matters. Do I really need it to be substitutable for 'file' after all ? I need to sit in a darkened room and think for a while... Cheers Duncan _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor