Type subclassing: bug or feature
Bjorn Pettersen
BPettersen at NAREX.com
Thu Jun 13 17:06:01 EDT 2002
More information about the Python-list mailing list
Thu Jun 13 17:06:01 EDT 2002
- Previous message (by thread): Type subclassing: bug or feature
- Next message (by thread): Type subclassing: bug or feature
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
> From: Aahz [mailto:aahz at pythoncraft.com] > > 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? Well, in e.g. C++ this would make perfect sense, since a base class method can't know the real type of self/this. In Python we can do better of course: >>> class A: ... def foo(self): ... return self.__class__() ... >>> class B(A): ... pass ... >>> b = B() >>> b.foo() <__main__.B instance at 0x00864CC0> which version is correct is mostly a religious question, but personally I think that if we _can_ do better than C++ we should <wink>. File a bug and see what Guido says... -- bjorn
- Previous message (by thread): Type subclassing: bug or feature
- Next message (by thread): Type subclassing: bug or feature
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list