[Tutor] class Writer
Christopher Spears
cspears2002 at yahoo.com
Thu May 25 01:34:53 CEST 2006
I've been working my way through an online tutorial
and came across the following sample script:
import sys
class Writer:
def __init__(self, filename):
self.filename = filename
def write(self, msg):
f = file(self.filename, 'a')
f.write(msg)
f.close()
sys.stdout = Writer('tmp.log')
print 'Log message #1'
print 'Log message #2'
print 'Log message #3'
The script created the tmp.log file with the following
lines:
Log message #1
Log message #2
Log message #3
I understand that the class is taking the strings from
stdout (supplied by the print statements) and writing
them to a text file. Does the user need to explicitly
call the write function? For example:
sys.stdout = Writer('tmp.log').write(whatever the
message is)
More information about the Tutor
mailing list