better way than: myPage += 'more html' , ...

Gerrit Holl gerrit at nl.linux.org
Thu Jun 26 11:25:10 CEST 2003


Delaney, Timothy C (Timothy) wrote:
> import cStringIO as StringIO
> 
> # Subclass of str so that it can be used nearly anywhere that requires a real
> # string.
> 
> class MutableString (str):
> 
>     def __init__ (self, s=""):
>         super(MutableString, self).__init__()
>         self.data = StringIO.StringIO()
>         self.data.write(s)
> 
>     def __add__(self, s):
>         m = MutableString(self.data.getvalue())
>         m.data.write(s)
>         return m
> 
>     def __iadd__(self, s):
>         self.data.write(s)
>         return self
> 
>     def __str__(self):
>         return self.data.getvalue()
> 
>     def __repr__(self):
>         return repr(str(self))

You could even add __setitem__ and __getitem__ and implement it with
.seek.

yours,
Gerrit.

-- 
62. If he do not plant the field that was given over to him as a
garden, if it be arable land (for corn or sesame) the gardener shall pay
the owner the produce of the field for the years that he let it lie
fallow, according to the product of neighboring fields, put the field in
arable condition and return it to its owner.
        -- 1780 BC, Hammurabi, Code of Law
--
Asperger Syndroom - een persoonlijke benadering:
	http://people.nl.linux.org/~gerrit/
Het zijn tijden om je zelf met politiek te bemoeien:
	http://www.sp.nl/





More information about the Python-list mailing list