Type subclassing: bug or feature
not.this at seebelow.org
Fri Jun 14 00:34:22 CEST 2002
In article <aeau48$111$1 at panix1.panix.com>, aahz at pythoncraft.com says...
>Consider the following code:
> def contains(self, value):
> return self.find(value) >= 0
>s = MyStr("hello, world!")
>s = s.capitalize()
> 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
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.
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