subclassing "file"

Peter Otten __peter__ at web.de
Mon Jan 19 12:08:14 EST 2004


Oops, hit the wrong button. The example goes like so:

>>> class myfile(file):
...     def __init__(self, op, *args):
...             file.__init__(self, *args)
...             self.op = op
...     def write(self, s):
...             file.write(self, self.op(s))
...
>>> f = myfile(lambda s: "-".join(s), "tmp.txt", "w")
>>> f.write("a foolish consciousness")
>>> f.close()
>>> file("tmp.txt").read()
'a- -f-o-o-l-i-s-h- -c-o-n-s-c-i-o-u-s-n-e-s-s'
>>>

Of course I used *args only because I'm to laze to name the actual arguments
one at a time :-)

Peter





More information about the Python-list mailing list