Python 2.6 / 3.0: Determining if a method is inherited

Fuzzyman fuzzyman at gmail.com
Mon Oct 6 20:52:40 CEST 2008


On Oct 6, 7:23 pm, "Aaron \"Castironpi\" Brady" <castiro... at gmail.com>
wrote:
> On Oct 6, 1:17 pm, Fuzzyman <fuzzy... at gmail.com> wrote:
>
>
>
> > On Oct 6, 7:01 pm, "Aaron \"Castironpi\" Brady" <castiro... at gmail.com>
> > wrote:
> > > It's a very object oriented solution.  Essentially you're inheriting
> > > all the classes that you want to fail, from a class that does.
>
> > But not a very good solution to the problem...
>
> > The specific problem is to determine if an arbitrary class implements
> > a specified comparison method. The general problem (that gives rise to
> > the specific problem) is to write a class decorator that can implement
> > all comparison methods from a class that implements only one.
>
> > See:http://code.activestate.com/recipes/576529/
>
> > Michael
> > --http://www.ironpythoninaction.com/
>
> Nope, I'm out of ideas, I'm afraid.

Thankfully that page I pointed you to has the solution I came up with
- walk the method resolution order of the class checking in the
classes' '__dict__' to see what they explicitly implement.

Given that you can get hold of X.__lt__ I was surprised by how hard it
was to tell whether that was an inherited implementation or not.

Michael
--
http://www.ironpythoninaction.com



More information about the Python-list mailing list