Type subclassing: bug or feature

Aahz aahz at pythoncraft.com
Thu Jun 13 16:08:40 EDT 2002


Consider the following code:

class MyStr(str):
    def contains(self, value):
        return self.find(value) >= 0

s = MyStr("hello, world!")
s = s.capitalize()
if s.contains('Hello'):
    print "Found it!"

It fails with an AttributeError when it calls s.contains(), because
s.capitalize() returned a str instead of a MyStr.  Anyone want to take a
whack at defending this as the correct behavior?
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"I had lots of reasonable theories about children myself, until I
had some."  --Michael Rios



More information about the Python-list mailing list