Newbie needs "interface"
Fredrik Lundh
fredrik at pythonware.com
Thu Nov 22 14:31:19 EST 2001
Roberto Bonato wrote:
> I'm writing a function that implements an algorithm and that
> prints intermediate results all along the (usually very long)
> computation. Sometimes I want the results to be written on the stdout,
> sometimes on a Tkinter Text widget, sometimes to a file. I feel the need
> of something like a Java Interface with a method "write" to wrap all
> such devices into.
in python, interfaces are informal, so all you have to do is to
create something with a "write" method. for example:
import Tkinter
class TextWithWrite(Tkinter.Text):
def write(self, string):
self.insert(Tkinter.END, string)
self.see(Tkinter.END)
self.update_idletasks() # redraw me
if print_to_file:
outfile = open(myfile, "w")
else:
w = TextWithWrite(master)
w.pack()
outfile = w
print >>outfile, "something"
print >>outfile, "something else"
</F>
<!-- (the eff-bot guide to) the python standard library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->
More information about the Python-list
mailing list