Writing to stdout and a log file
Michael Hoffman
cam.ac.uk at mh391.invalid
Wed Apr 20 08:55:41 EDT 2005
Mike wrote:
> I should've mentioned I want StdoutLog to subclass the 'file' type
> because I need all the file attributes available.
You might use a surrogate pattern. Here's one I use for this kind of
situation, where I want to subclass but that can't be done for some reason.
class SurrogateNotInitedError(exceptions.AttributeError):
pass
class Surrogate(object):
def __init__(self, data):
self._data = data
def __getattr__(self, name):
if name == "_data":
raise SurrogateNotInitedError, name
else:
try:
return getattr(self._data, name)
except SurrogateNotInitedError:
raise SurrogateNotInitedError, name
I'll leave it as an exercise to the reader to make this work when
self._data is actually a list of objects instead of a single object.
You'll obviously need special logic for different methods, like write(),
since for some of them you will want to call every object in self._data,
and others only a single object.
--
Michael Hoffman
More information about the Python-list
mailing list