在python中迭代对象属性[重复]
- 2025-03-04 08:25:00
- admin 原创
- 94
问题描述:
我有一个具有多个属性和方法的 Python 对象。我想迭代对象属性。
class my_python_obj(object):
attr1='a'
attr2='b'
attr3='c'
def method1(self, etc, etc):
#Statements
我想生成一个包含所有对象属性及其当前值的字典,但我想以动态的方式进行(因此,如果以后我添加另一个属性,则不必记住更新我的函数)。
在 php 中变量可以用作键,但 python 中的对象是不可引用的,如果我对此使用点符号,它会创建一个以我的变量名称命名的新属性,这不是我的意图。
只是为了让事情更清楚:
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
更新:我说的属性仅仅是指这个对象的变量,而不是方法。
解决方案 1:
假设你有一个类,例如
>>> class Cls(object):
... foo = 1
... bar = 'hello'
... def func(self):
... return 'call me'
...
>>> obj = Cls()
调用dir
该对象将返回该对象的所有属性,包括 Python 特殊属性。尽管有些对象属性是可调用的,例如方法。
>>> dir(obj)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'bar', 'foo', 'func']
您可以随时使用列表推导来过滤掉特殊方法。
>>> [a for a in dir(obj) if not a.startswith('__')]
['bar', 'foo', 'func']
或者如果您更喜欢地图/过滤器。
>>> filter(lambda a: not a.startswith('__'), dir(obj))
['bar', 'foo', 'func']
如果您想过滤掉这些方法,您可以使用内置方法callable
作为检查。
>>> [a for a in dir(obj) if not a.startswith('__') and not callable(getattr(obj, a))]
['bar', 'foo']
您还可以使用以下方法检查您的类与其实例对象之间的差异。
>>> set(dir(Cls)) - set(dir(object))
set(['__module__', 'bar', 'func', '__dict__', 'foo', '__weakref__'])
解决方案 2:
一般来说,在你的类__iter__
中放置一个方法并遍历对象属性,或者把这个混合类放在你的类中。
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'}
解决方案 3:
正如一些答案/评论中已经提到的,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
)。
解决方案 4:
python 中的对象将其属性(包括函数)存储在名为 的字典中__dict__
。您可以(但通常不应该)使用它直接访问属性。如果您只想要一个列表,也可以调用dir(obj)
,它会返回一个包含所有属性名称的可迭代对象,然后您可以将其传递给getattr
。
但是,需要对变量名称进行任何操作通常都是不好的设计。为什么不将它们保存在集合中呢?
class Foo(object):
def __init__(self, **values):
self.special_values = values
然后你可以使用for key in obj.special_values:
解决方案 5:
class SomeClass:
x = 1
y = 2
z = 3
def __init__(self):
self.current_idx = 0
self.items = ["x", "y", "z"]
def next(self):
if self.current_idx < len(self.items):
self.current_idx += 1
k = self.items[self.current_idx-1]
return (k, getattr(self, k))
else:
raise StopIteration
def __iter__(self):
return self
然后将其称为可迭代对象
s = SomeClass()
for k, v in s:
print k, "=", v
解决方案 6:
对于 Python 3.6
class SomeClass:
def attr_list(self, should_print=False):
items = self.__dict__.items()
if should_print:
[print(f"attribute: {k} value: {v}") for k, v in items]
return items
解决方案 7:
正确的答案是,你不应该这样做。如果你想要这种东西,要么只使用字典,要么你需要明确地向某个容器添加属性。你可以通过学习装饰器来自动化这一过程。
顺便说一下,特别地,您示例中的 method1 是一种同样好的属性。
扫码咨询,免费领取项目管理大礼包!