Python 2.6: Determining if a method is inherited

Valentino Volonghi aka Dialtone dialUAZ###UZ#$AAtone at gWARAmail.com
Sun Oct 5 21:15:36 CEST 2008


Fuzzyman <fuzzyman at gmail.com> wrote:

> So how do I tell if the X.__lt__ is inherited from object? I can look

I don't have python 2.6 installed so I can't try but what I think could
work is:

>>> class Foo(object):
...     def hello(self):
...         pass
... 
>>> class Bla(Foo):
...     def hello(self):
...         pass
... 
>>> class Baz(Foo):
...     pass
... 
>>> Baz.hello.im_func
<function hello at 0x2b1630>
>>> Bla.hello.im_func
<function hello at 0x2b1670>
>>> Foo.hello.im_func
<function hello at 0x2b1630>
>>> Foo.hello.im_func is Baz.hello.im_func
True
>>> Foo.hello.im_func is Bla.hello.im_func
False

Which to me also makes sense. If it's inherited I expect it to be the
very same function object of one of the bases (which you can get with
inspect.getmro(Clazz)). On the other end if you override it it's not the
same function object so it won't return True when applied to 'is'.

HTH

-- 
Valentino Volonghi aka Dialtone
http://stacktrace.it -- Aperiodico di resistenza informatica
Blog: http://www.twisted.it/
Public Beta: http://www.adroll.com/



More information about the Python-list mailing list