Python 2.6: Determining if a method is inherited

Terry Reedy tjreedy at udel.edu
Mon Oct 6 20:02:23 CEST 2008


Fuzzyman wrote:
> On Oct 5, 11:54 pm, Terry Reedy <tjre... at udel.edu> wrote:
>> 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.

> So long as its never used on a class with 'object' in the name.
> Doesn't sound like a particularly *good* solution to me. :-)

 From what you posted, 'type object at' should work.




More information about the Python-list mailing list