Type subclassing: bug or feature

Tim Peters tim.one at comcast.net
Thu Jun 13 22:09:16 EDT 2002


[Aahz]
> 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?

Short course:

    http://www.python.org/sf/468887

Long course:

    http://www.python.org/sf/460020

Making sure this pisses you off in all cases required one of the biggest
patch sets that went into Python 2.2 <wink>.






More information about the Python-list mailing list