打印对象的属性

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

#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