您的位置:首页 > 其它

使用dir列出包含继承的属性

2017-10-15 23:50 85 查看

lister1.py

class ListInherited:
def __str__(self):
return '<Instance of %s,address %s:\n%s>'%(
self.__class__.__name__,
id(self),
self.__attrnames())

def __attrnames(self):
result = ''
for attr in dir(self):
if attr[:2] == '__' and attr[-2:] == '__':
result += '\tname %s=<>\n'%attr
else:
result +='\tname %s=%s\n'%(attr,getattr(self,attr))
return result


测试:

from lister1 import *

class Super:
def __init__(self):
self.data1 = 'spam'
def ham(self):
pass

class Sub(Super,ListInherited):
def __init__(self):
Super.__init__(self)
self.data2 = 'eggs'
self.data3 = 42

def spam(self):
pass

if __name__ == '__main__':
X = Sub()
print(X)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐