[Tutor] question about operator overloading

Steven D'Aprano steve at pearwood.info
Tue Mar 6 01:58:45 CET 2012


Alan Gauld wrote:
> On 05/03/12 21:25, Dave Angel wrote:
> 
>> It's not clear what __add__() should mean for physical files.
> 
> My guess would be similar to the cat operator in Unix:
> 
> $ cat file1, file2 > file3
> 
> is equivalent to
> 
> file3 = file1 + file2
> 
> But of course, thats just my interpretation of file addition...

I think that's what Albert-Jan is probably thinking, but the two models are 
not quite the same. I think that what he wants is probably closer to something 
like the fileinput module. I think what he wants is to avoid this:

for f in (file1, file2, file3, file4):
     for record in f:
         process(record)

in favour of this:

all_the_files = file1 + file2 + file3 + file4  # merge file contents
for record in all_the_files:
     process(record)

Albert-Jan, am I close? If not, please explain what you are trying to accomplish.

If the files are small, the easy way is to just read their contents, add them 
together as strings or lists, and then process the lot. But if the files are 
big, or you want to process them on-demand instead of up-front, you need an 
approach similar to fileinput.

Personally, all these Reader and Append objects make my brain hurt, and I 
hardly ever use operator overloading, except perhaps for numeric types. Reader 
objects, I can just get. But "Append" objects?

This may be useful:

http://steve-yegge.blogspot.com.au/2006/03/execution-in-kingdom-of-nouns.html

and also itertools:


from itertools import chain
merged = chain(file1, file2, file3, file4)
for record in merged:
     process(record)



-- 
Steven


More information about the Tutor mailing list