python打印一个对象的所有属性

如下↓

class Data(object):
    def __init__(self):
        self.value1 = 'caixukun'
        self.value2 = 'wuyifan'
        self.value3 = 'mabaoguo'


if __name__ == '__main__':
    
    # 随便声明一个类
    data = Data()
    
    # 打印它的全部属性
    print('\n'.join(['{0}: {1}'.format(item[0], item[1]) for item in data.__dict__.items()]))
    # value1: caixukun
	# value2: wuyifan
	# value3: mabaoguo

你可能感兴趣的:(Python,python,pycharm)