string methods of a str subclass

Daniel Nogradi nogradi at gmail.com
Mon Apr 16 05:28:58 EDT 2007


I am probably misunderstanding some basic issue here but this
behaviour is not what I would expect:



Python 2.4 (#1, Mar 22 2005, 21:42:42)
[GCC 3.3.5 20050117 (prerelease) (SUSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class mystr( str ):
...     pass
...
>>> x = mystr( 'x' )
>>> isinstance( x, mystr )
True
>>> isinstance( x.strip( ), mystr )
False
>>>


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. How would I achieve something
like this without manually copying all string returning methods from
str and stuffing the result to mystr( ) before returning?



More information about the Python-list mailing list