Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Can a base class know if a method has been overridden?

Reply
Thread Tools

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

 
 
Ratko
Guest
Posts: n/a
 
      09-24-2007
On Sep 24, 12:56 pm, Stéphane Larouche <(E-Mail Removed)>
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

 
Reply With Quote
 
 
 
 
Steven Bethard
Guest
Posts: n/a
 
      09-24-2007
Ratko wrote:
> I was wondering if something like this is possible. Can a base class
> somehow know if a certain method has been overridden by the subclass?


You can try using the __subclasses__() method on the class::

>>> def is_overridden(method):

... for cls in method.im_class.__subclasses__():
... func = cls.__dict__.get(method.__name__)
... if func is not None and func != method.im_func:
... return True
... return False
...
>>> class A(object):

... def foo(self):
... return 'A.foo'
... def bar(self):
... return 'A.bar'
...
>>> class B(A):

... def foo(self):
... return 'B.foo'
...
>>> is_overridden(A.foo)

True
>>> is_overridden(A.bar)

False

Given a method from the base class, is_overridden gets the base class,
and then looks through every subclass of that class. If the function
object for the subclass is ever different from that of the base class,
then the base class's method must have been overridden.

STeVe
 
Reply With Quote
 
 
 
 
Raymond Hettinger
Guest
Posts: n/a
 
      09-24-2007
On Sep 24, 8:23 am, Ratko <(E-Mail Removed)> wrote:
> Hi all,
>
> I was wondering if something like this is possible. Can a base class
> somehow know if a certain method has been overridden by the subclass?
> I appreciate any ideas.
> Thanks,
>
> Ratko



It's not hard. Both bound and unbound methods have an im_func
attribute pointing to the underlying function code. So you can check
for overrides by testing whether the underlying functions are the
same:

>>> class A:

def p(self): pass
def q(self): pass


>>> class B(A):

def q(self): pass


>>> b = B()
>>> b.p.im_func is A.p.im_func # Verify that b.p runs A.p

True
>>> b.q.im_func is A.q.im_func # Verify that b.q overrides A.q




Raymond

False

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Base class method Need base class value Karan Rajput Ruby 2 12-22-2010 04:47 PM
why a class can't access protected method from another class in thesame package,the method is interited from the ohtner class from differntpackage? junzhang1983@gmail.com Java 3 01-28-2008 02:09 AM
know can I know how much object has been created??? sayoyo@yahoo.com Ruby 2 03-31-2006 08:09 PM
The printing has been stopped and this job has been add to the queu? dejola Computer Support 6 12-30-2005 03:26 AM
How can we know if a HttpServletResponse has been redirected ? Bruno Thomas Java 2 09-13-2005 02:47 PM



Advertisments