python 打印对象的属性
当前位置:以往代写 > Python教程 >python 打印对象的属性
2019-06-14

python 打印对象的属性

python 打印对象的属性
python">#coding=utf-8
class AttrDisplay:
    def getAttrs(self):
        res = []
        for key in self.__dict__:
            res.append("%s=%s"%(key, getattr(self, key)))
        return "  ".join(res)
    def __str__(self):
        # attrs = AttrDisplay.getAttrs(self)
        return "[%s: %s]"%(self.__class__.__name__, self.getAttrs())

if __name__ == "__main__":
    class TopTest(AttrDisplay):
        count = 0
        name = "math"
        def __init__(self):
            self.attr1 = TopTest.count
            self.attr2 = TopTest.count + 1
            self.attr3 = TopTest.name
            TopTest.count += 2
            TopTest.name = 'English'

    class SubTest(TopTest):
        pass

    x = TopTest()
    print x
    y = SubTest()
    print y
    Z = SubTest()
    print Z

标签:python

    关键字:

在线提交作业