string methods of a str subclass

Duncan Booth duncan.booth at invalid.invalid
Mon Apr 16 12:01:30 CEST 2007


"Daniel Nogradi" <nogradi at gmail.com> wrote:

> Why is the strip( ) method returning something that is not a mystr
> instance? I would expect all methods operating on a string instance
> and returning another string instance to correctly operate on a mystr
> instance and return a mystr instance.

Why would you expect that?
Would you expect the __str__ and__repr__ methods also to return a mystr 
instance? If not those, then which other ones might also be excluded?
Is x.encode('zip') still a mystr instance or an encoded byte-string?

> How would I achieve something
> like this without manually copying all string returning methods from
> str and stuffing the result to mystr( ) before returning?

You don't without wrapping all the affected methods. It doesn't need to 
involve manual copying though: you have a programming language available so 
just write a list of method names and then some code to wrap them 
automatically.



More information about the Python-list mailing list