开发者

How to determine from which class a method was inherited from?

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?


Use the inspect module:

class A:
   def foo(self):
     pass
class B(A):
   def boo(Aself):
     pass
class C(B):
   def coo(self):
     pass
class D(C):
   def doo(self):
      pass  

import inspect 
inspect.classify_class_attrs(D)

[('__doc__', 'data', <class __main__.D at 0x85fb8fc>, None),
 ('__module__', 'data', <class __main__.D at 0x85fb8fc>, '__main__'),
 ('boo',
  'method',
  <class __main__.B at 0x85fb44c>,
  <function boo at 0x8612bfc>),
 ('coo',
  'method',
  <class __main__.C at 0x85fb8cc>,
  <function coo at 0x8612ca4>),
 ('doo',
  'method',
  <class __main__.D at 0x85fb8fc>,
  <function doo at 0x8612f0c>),
 ('foo',
  'method',
  <class __main__.A at 0x85fb71c>,
  <function foo at 0x8612f7c>)]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜