Thanks guys for the responses,

inspect.classify_class_attrs(klass)

does the magic

Regards





On Wed, Jan 19, 2011 at 3:58 PM, Peter Otten <__pete...@web.de> wrote:

> Jojo Mwebaze wrote:
>
> > Is it possible to tell, from which class an method was inherited from.
> > take an example below
> >
> > class A:
> >    def foo():
> >      pass
> > class B(A):
> >    def boo(A):
> >      pass
> > class C(B):
> >    def coo()
> >      pass
> > class D(C):
> >    def doo()
> >       pass
> >
> >>>> dir (D)
> > ['__doc__', '__module__', 'boo', 'coo', 'doo', 'foo']
> >
> > Is there any method to tell me form which classes boo, coo, foo where
> > inherited from?
>
> You can check the classes in method resolution order (mro):
>
>
> import inspect
>
> class A:
>    def foo(self):
>        pass
>    def bar(self):
>        pass
>
> class B(A):
>    def bar(self):
>        pass
>
> class C(B):
>    def baz(self):
>        pass
>
> for methodname in dir(C) + ["spam"]:
>    for class_ in inspect.getmro(C):
>        if methodname in class_.__dict__:
>            print "%s.%s()" % (class_.__name__, methodname)
>            break
>    else:
>        print "no attribute named %r found" % methodname
>
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to