print >> to a derived file

Ben Fisher jamon.ben at gmail.com
Tue Jan 15 05:44:33 EST 2008


This might have something to do with the class being derived from file.

I've written it so that it doesn't derive from file, and it works.

class File_and_console():
	def __init__(self, *args):
		self.fileobj = open(*args)
	def write(self, s):
		self.fileobj.write(s)
		print s,
	
f = File_and_console('testout.tmp','w')
f.write('hello')
print >>f,'hello',


On 1/15/08, iu2 <israelu at elbit.co.il> wrote:
> Hi,
>
> I'm trying to write data to both a file and the console, so I did:
>
> class File_and_console(file):
>        def write(self, s):
>                file.write(self, s)
>                print s,
>
> >>> f = File_and_console('1.txt', 'w')
> >>> f.write('hello')
> hello
> >>> print >>f, 'world'
> >>>
>
> the 'write' method works, but 'print >>' doesn't, it writes only to
> the file. It doesn't actually call File_and_console.write
>
> Why? How can I fix it?
> Thanks
> iu2
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list