Python 2.6 / 3.0: Determining if a method is inherited

Aaron "Castironpi" Brady castironpi at gmail.com
Sun Oct 5 21:57:33 EDT 2008


On Oct 5, 7:13 pm, MRAB <goo... at mrabarnett.plus.com> 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.
>
> > Hmmm... I can get this working with Python 2.6 with:
>
> Methods are objects. How do you know if two references refer to the
> same object? You use "is":
>
> X.__lt__ is object.__lt__

That doesn't work for me.

>>> class A( object ):
...     pass
...
>>> class B( A ):
...     def __lt__( self, other ):
...             return self
...
>>> a= A()
>>> b= B()
>>> B.__lt__ is object.__lt__
False
>>> A.__lt__ is object.__lt__
False
>>>

Further, it's been noted before that

    A().meth is not A().meth



More information about the Python-list mailing list