[Tutor] question about operator overloading
Alan Gauld
alan.gauld at btinternet.com
Tue Mar 6 09:48:25 CET 2012
On 06/03/12 02:42, Dave Angel wrote:
>> My guess would be similar to the cat operator in Unix:
>>
>> file3 = file1 + file2
>>
>
> So somehow assigning the object to file3 will write the data to a file
> by the name "file3" ? I know about __add__(), but didn't know we had
> __assign__()
We don't need any special assign behavior, its just standard Python
assignment of the returned object to a name.
class MyFile(file):
....
def __add__(self, file2):
newFile = MyFile('foo.dat','wb')
newFile.write(self.read())
newFile.write(file2.read())
return newFile
file3 = MyFile('spam.dat') + MyFile('baz.dat')
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list