Python 2.6: Determining if a method is inherited

Terry Reedy tjreedy at udel.edu
Sun Oct 5 18:54:10 EDT 2008


Fuzzyman wrote:
> Hello all,
> 
> I may well be being dumb (it has happened before), but I'm struggling
> to fix some code breakage with Python 2.6.
> 
> I have some code that looks for the '__lt__' method on a class:
> 
> if hasattr(clr, '__lt__'):
> 
> However - in Python 2.6 object has grown a default implementation of
> '__lt__', so this test always returns True.
> 
>>>> class X(object): pass
> ...
>>>> X.__lt__
> <method-wrapper '__lt__' of type object at 0xa15cf0>
>>>> X.__lt__ == object.__lt__
> False

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.

tjr




More information about the Python-list mailing list