Type subclassing: bug or feature
Grant Griffin
not.this at seebelow.org
Thu Jun 13 18:34:22 EDT 2002
In article <aeau48$111$1 at panix1.panix.com>, aahz at pythoncraft.com says...
>
>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?
Sounds like a bug to me, but perhaps fixing it at some point was part of the
plan from the beginning.
Editorially speaking, although I hadn't thought of this problem specificially, I
guess it might be another reason to use subclassing to augment behavior, not
change it.
I haven't figured out whether this sort of thing could happen with other, more
usefully subclassed types like list and dict, but I guess it could occur
whenever a built-in type returns its own (er, "parent") type. I bet somebody
can think of an example.
straight-man-ly y'rs,
=g2
_________________________________________________________________________
Grant R. Griffin g2 at dspguru.com
Publisher of dspGuru http://www.dspguru.com
Iowegian International Corporation http://www.iowegian.com
More information about the Python-list
mailing list