Can a base class know if a method has been overridden?

Ratko rjagodic at gmail.com
Mon Sep 24 15:17:48 EDT 2007


On Sep 24, 12:56 pm, Stéphane Larouche <stephane.larou... at polymtl.ca>
wrote:
> What about something like:
>
> class A(object):
>         def my_method(self):
>                 print "A.my_method"
>         def call_my_method(self):
>                 if type(self).my_method == A.my_method:
>                         print "Calling base class method."
>                 else:
>                         print "Calling derived class method."
>                 self.my_method()
>
> class B(A):
>         pass
>
> class C(A):
>         def my_method(self):
>                 print "C.my_method"
>
> a = A()
> b = B()
> c = C()
>
> a.call_my_method()
> b.call_my_method()
> c.call_my_method()
>
> Stéphane


I think it would confuse the user to have to call "call_my_method" as
opposed to calling "my_method" directly. The only reason why I wanted
to do this is simplicity, clarity and transparency. Thanks though.
Bruno's solution does exactly what I was looking for.

Ratko




More information about the Python-list mailing list