我有一个带有几个属性和方法的python对象。我想要遍历对象属性。
class my_python_obj(object):
attr1='a'
attr2='b'
attr3='c'
def method1(self, etc, etc):
#Statements
我想生成一个包含所有对象属性及其当前值的字典,但我想以一种动态的方式来做(因此,如果后来我添加了另一个属性,我不必记得更新我的函数)。
在php中,变量可以用作键,但python中的对象是不可感知的,如果我使用点符号,它会创建一个名为我的var的新属性,这不是我的意图。
为了让事情更清楚:
def to_dict(self):
'''this is what I already have'''
d={}
d["attr1"]= self.attr1
d["attr2"]= self.attr2
d["attr3"]= self.attr3
return d
·
def to_dict(self):
'''this is what I want to do'''
d={}
for v in my_python_obj.attributes:
d[v] = self.v
return d
更新:
这里的属性指的是这个对象的变量,而不是方法。
正如已经在一些回答/评论中提到的,Python对象已经存储了它们的属性字典(方法不包括在内)。这可以通过__dict__访问,但更好的方法是使用vars(尽管输出是相同的)。注意,修改这个字典将修改实例上的属性!这可能很有用,但也意味着您应该注意如何使用该词典。这里有一个简单的例子:
class A():
def __init__(self, x=3, y=2, z=5):
self.x = x
self._y = y
self.__z__ = z
def f(self):
pass
a = A()
print(vars(a))
# {'x': 3, '_y': 2, '__z__': 5}
# all of the attributes of `a` but no methods!
# note how the dictionary is always up-to-date
a.x = 10
print(vars(a))
# {'x': 10, '_y': 2, '__z__': 5}
# modifying the dictionary modifies the instance attribute
vars(a)["_y"] = 20
print(vars(a))
# {'x': 10, '_y': 20, '__z__': 5}
使用dir(a)是解决这个问题的一种奇怪的(如果不是完全糟糕的话)方法。如果你真的需要遍历类的所有属性和方法(包括__init__这样的特殊方法),这是很好的。然而,这似乎不是你想要的,即使是公认的答案也很糟糕,它应用了一些脆弱的过滤,试图删除方法,只留下属性;你可以看到这对于上面定义的A类是如何失败的。
(在一些回答中使用__dict__,但它们都定义了不必要的方法,而不是直接使用它。只有一个注释建议使用vars)。
一般来说,在类中放入__iter__方法并遍历对象属性,或者将这个mixin类放入类中。
class IterMixin(object):
def __iter__(self):
for attr, value in self.__dict__.iteritems():
yield attr, value
你的类:
>>> class YourClass(IterMixin): pass
...
>>> yc = YourClass()
>>> yc.one = range(15)
>>> yc.two = 'test'
>>> dict(yc)
{'one': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14], 'two': 'test'}