Print attribute of class based on attribute name
本问题已经有最佳答案,请猛点这里访问。
is there to print out the方式的属性对象在Python with their of a值P></
for example for the following,类和对象P></
1 2 3 4 5 6 | class man: def __init__(self): name ="jim" age = 2 him = man() |
我想在打印出的"对象属性"他"As of the values as their阱。我想要一些kind of that like the following队列行为P></
1 2 | for variable_name of him: print variable_name," :",him.variable_name |
should打印超时P></
1 2 | name : jim age : 2 |
is there any to do that在Python的方式?P></
首先,您需要正确定义对象中的字段:
1 2 3 4 | class man(objet): def __init__(self): self.name ="jim" self.age = 2 |
然后可以使用
1 2 3 | >>> him = man() >>> himp.__dict__ {'name': 'jim', 'age': 2} |