Problem deriving a class from the built-in file object

Pierre Rouleau Pierre_Rouleau at impathnetworks.com
Fri Apr 22 12:12:07 EDT 2005


Peter Otten wrote:
> Pierre Rouleau wrote:
> 
> 
>>I'm trying to extend the functionality of the file object by creating a
>>class that derives from file.  MyFile class re-implements __init__(),
>>write(), writelines() and close() to augment the capabilities of file.
>>
>>All works fine, except for one thing:  'print >> myfile'  does not
>>execute Myfile.write(), it executes the file.write().   If I execute
>>myfile.write() explicitly, then Myfile.write() is called as expected.
> 
> 
> As a workaround, you can use delegation instead of inheritance:
> 
> 

Good idea, thanks!

>>Is it the expected behavior?
> 
> 
> I certainly didn't expect it either when I saw it for the first time.
> 
> Peter
> 

Is it something that will (or should) be changed in future version of 
Python?  I can't see any reason to keep the current behaviour.

Pierre



More information about the Python-list mailing list