overriding method that returns base class object
Peter Hansen
peter at engcorp.com
Mon Feb 16 17:45:47 EST 2004
Stuart McGraw wrote:
>
> I have a class A from a third party that I cannot change
> and is implemented in C. I derive my own class B from A
> and add a couple new methods and override a method. The
> problem is that A has a method (call it A.f() ) that creates
> and returns a new A object. I need B.f() to return a B
> object derived from A.f(). What is the best way to make
> that happen?
If I understand this correctly, it has nothing to do with the
fact that the parent class is implemented in C and you just
need to know a little uncommon syntax:
class A:
def f(self):
return A()
class B(A):
def f(self):
obj = A.f(self)
# do whatever you want to obj here
return obj
The key is what you mean by "a B object derived from A.f()". If
by derived you mean something to do with _inheritance_, then
either you don't understand inheritance or you weren't clear what
you wanted.
If you just mean you want B's f() to do something special to the
A object that A.f() returns, then the above code should let you
do that properly...
-Peter
More information about the Python-list
mailing list