Python 2.6: Determining if a method is inherited

Christian Heimes lists at
Mon Oct 6 08:16:20 CEST 2008

Terry Reedy wrote:
> In 3.0, the test returns true because function attributes only get 
> wrapped when bound.  In the meanwhile, " 'object' in repr(X.__lt__)" 
> should do it for you.

This session should give you some hints how to archive your goal :)
Have fun!

 >>> import types
 >>> class A(object):
...     def __lt__(self):
...         pass
 >>> isinstance(A.__lt__, types.MethodType)
 >>> isinstance(A.__gt__, types.MethodType)

 >>> type(A.__lt__)
<type 'instancemethod'>
 >>> type(A.__gt__)
<type 'method-wrapper'>
 >>> type(A.__str__)
<type 'wrapper_descriptor'>
 >>> type(A.__new__)
<type 'builtin_function_or_method'>

 >>> A.__lt__.im_func
<function __lt__ at 0x7f03f9298500>
 >>> A.__lt__.im_func == A.__lt__.im_func
 >>> A.__gt__.im_func
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
AttributeError: 'method-wrapper' object has no attribute 'im_func'

More information about the Python-list mailing list