1 python列出多重继承类的每个对象属性
1.1 列出实例属性
python通过__dict__列出实例属性。
(1) 实例.__class__:获取实例所属类。
(2) 类.__name__:获取类的名称。
(3) 实例.__class__.__name__:获取实例的类的名称。
(4) 实例.__dict__:获取实例的属性字典。
(5) id(实例):获取实例的内存地址。
(6) __attrnames:伪私有属性,自动扩展为_ListInstance__attrnames。
(7) __str__:重载__str__,使用print时自动调用__str__。
示例
lister.py
class ListInstance:def __str__(self):return '<Instance of {},address {}:\n{}>'.format(self.__class__.__name__,id(self),self.__attrnames())def __attrnames(self):result=''for attr in sorted(self.__dict__):result+='\tname {}={}\n'.format(attr,self.__dict__[attr])return result
单继承print调用__str__
>>> import os
>>> os.chdir(r'E:\documents\F盘')
>>> from lister import ListInstance
>>> class PubAcct(ListInstance):def __init__(self):self.name='梯阅线条'
>>> pa=PubAcct()
>>> print(pa)
<Instance of PubAcct,address 61570576:name name=梯阅线条
>
>>> pa
<__main__.PubAcct object at 0x03AB7E10>
>>> str(pa)
'<Instance of PubAcct,address 61570576:\n\tname name=梯阅线条\n>'
testmulclass.py
from lister import *
class BaseC1:def __init__(self):self.title1='梯阅线条'def m1(self):passclass SubC1(BaseC1,ListInstance):def __init__(self):super().__init__()self.title2='tyxt.work'self.title3='软件测试开发'def m2(self):passif __name__=='__main__':sc1=SubC1()print(sc1)
多继承print调用__str__
>>> import os
>>> os.chdir(r'E:\documents\F盘')
>>> from testmulclass import *
>>> sc1=SubC1()
>>> print(sc1)
<Instance of SubC1,address 60595344:name title1=梯阅线条name title2=tyxt.workname title3=软件测试开发
>
1.2 列出继承属性
python通过dir(实例)列出实例及其基类的属性。
(1) attr[:2]和attr[-2:]去掉内置属性:双下化下开头和结尾的属性。
(2) getattr(实例,属性名):获取实例属性,包括继承属性。
示例
lister.py
class ListInherited:def __str__(self):return '<Instance of {},address {}:\n{}>'.format(self.__class__.__name__,id(self),self.__attrnames())def __attrnames(self):result=''for attr in dir(self):if attr[:2]=='__' and attr[-2:]=='__':result+='\tname {}=<>\n'.format(attr)else:result+='\tname {}={}\n'.format(attr,getattr(self,attr))return result
testmulclass.py
from lister import *
class BaseC1:def __init__(self):self.title1='梯阅线条'def m1(self):passclass SubC1(BaseC1,ListInstance):def __init__(self):super().__init__()self.title2='tyxt.work'self.title3='软件测试开发'def m2(self):passclass SubC2(BaseC1,ListInherited):def __init__(self):super().__init__()self.title2='tyxt.work'self.title3='软件测试开发'def m2(self):passif __name__=='__main__':sc1=SubC1()print(sc1)sc2=SubC2()print(sc2)
多继承print调用__str__
>>> import os
>>> os.chdir(r'E:\documents\F盘')
>>> sc2=SubC2()
Traceback (most recent call last):File "<pyshell#2>", line 1, in <module>sc2=SubC2()
NameError: name 'SubC2' is not defined
>>> from testmulclass import *
>>> sc2=SubC2()
>>> print(sc2)
<Instance of SubC2,address 57252176:name _ListInherited__attrnames=<bound method ListInherited.__attrnames of <testmulclass.SubC2 object at 0x03699950>>name __class__=<>name __delattr__=<>......name __subclasshook__=<>name __weakref__=<>name m1=<bound method BaseC1.m1 of <testmulclass.SubC2 object at 0x03699950>>name m2=<bound method SubC2.m2 of <testmulclass.SubC2 object at 0x03699950>>name title1=梯阅线条name title2=tyxt.workname title3=软件测试开发
>
1.3 列出每个对象的属性
python通过实例.__class__获取所属类,通过类.__bases__获取全部基类,通过实例.__dict__获取属性,从而获取每个对象的属性。
示例
lister.py
class ListTree:def __str__(self):self.__visited={}return '<Instance of {0},address {1}:\n{2}{3}>'.format(self.__class__.__name__,id(self),self.__attrnames(self,0),self.__listclass(self.__class__,4))def __listclass(self,aClass,indent):dots='.'*indentif aClass in self.__visited:return '\n{0}<Class {1}:,address {2}:(see above)>\n'.format(dots,aClass.__name__,id(aClass))else:self.__visited[aClass]=Truegenabove=(self.__listclass(c,indent+4) for c in aClass.__bases__)return '\n{0}<Class {1},address {2}:\n{3}{4}{5}>\n'.format(dots,aClass.__name__,id(aClass),self.__attrnames(aClass,indent),''.join(genabove),dots)def __attrnames(self,obj,indent):spaces=' '*(indent+4)result=''for attr in sorted(obj.__dict__):if attr.startswith('__') and attr.endswith('__'):result+=spaces+'{0}=<>\n'.format(attr)else:result+=spaces+'{0}={1}\n'.format(attr,getattr(obj,attr))return result
testmulclass.py
class SubC3(BaseC1,ListTree):def __init__(self):super().__init__()self.title2='tyxt.work'self.title3='软件测试开发'def m2(self):pass
多继承列出每个对象的属性
>>> import os
>>> os.chdir(r'E:\documents\F盘')
>>> from testmulclass import *
>>> sc3=SubC3()
>>> print(sc3)
<Instance of SubC3,address 57711792:_ListTree__visited={}title1=梯阅线条title2=tyxt.worktitle3=软件测试开发....<Class SubC3,address 57923944:__doc__=<>__init__=<>__module__=<>m2=<function SubC3.m2 at 0x0371B660>........<Class BaseC1,address 57922528:__dict__=<>__doc__=<>__init__=<>__module__=<>__weakref__=<>m1=<function BaseC1.m1 at 0x0371B4B0>............<Class object,address 1813587016:__class__=<>__delattr__=<>__dir__=<>__doc__=<>__eq__=<>__format__=<>__ge__=<>__getattribute__=<>__gt__=<>__hash__=<>__init__=<>__init_subclass__=<>__le__=<>__lt__=<>__ne__=<>__new__=<>__reduce__=<>__reduce_ex__=<>__repr__=<>__setattr__=<>__sizeof__=<>__str__=<>__subclasshook__=<>
............>
........>........<Class ListTree,address 57922056:_ListTree__attrnames=<function ListTree.__attrnames at 0x0371B468>_ListTree__listclass=<function ListTree.__listclass at 0x0371B420>__dict__=<>__doc__=<>__module__=<>__str__=<>__weakref__=<>............<Class object:,address 1813587016:(see above)>
........>
....>
>