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

So how do I tell if the X.__lt__ is inherited from object? I can look
in the '__dict__' of the class - but that doesn't tell me if X
inherits '__lt__' from a base class other than object. (Looking inside
the method wrapper repr with a regex is not an acceptable answer...)

They're of different types. I'm not sure how much you could use that, or how reliable it is.

>>> class A( object ):
...     pass
...
>>> class B( object ):
...     def __lt__( self, other ):
...             pass
...
>>> hasattr( A, '__lt__' )
True
>>> hasattr( B, '__lt__' )
True
>>> A.__lt__
<method-wrapper '__lt__' of type object at 0x00B81D48>
>>> B.__lt__
<unbound method B.__lt__>
>>>

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to