列出对象的属性 [英] List attributes of an object
本文介绍了列出对象的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法获取存在于类的实例上的属性列表? (这个类只是一个平淡的例子,这不是我手头的任务。)
Is there a way to grab a list of attributes that exist on instances of a class? (This class is just an bland example, it is not my task at hand.)
class new_class():
def __init__(self, number):
self.multi = int(number) * 2
self.str = str(number)
a = new_class(2)
print(', '.join(a.SOMETHING))
所需的结果是 multi,str将被输出。
The desired result is that "multi, str" will be output. I want this to see the current attributes from various parts of a script.
我使用的是Python 3。
I am using Python 3.
推荐答案
>>> class new_class():
... def __init__(self, number):
... self.multi = int(number) * 2
... self.str = str(number)
...
>>> a = new_class(2)
>>> a.__dict__
{'multi': 4, 'str': '2'}
>>> a.__dict__.keys()
dict_keys(['multi', 'str'])
您还可以找到 pprint 帮助。
这篇关于列出对象的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文