问题:遍历python中的对象属性
我有一个带有几个属性和方法的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
更新:对于属性,我的意思是仅此对象的变量,而不是方法。
I have a python object with several attributes and methods. I want to iterate over object attributes.
class my_python_obj(object):
attr1='a'
attr2='b'
attr3='c'
def method1(self, etc, etc):
#Statements
I want to generate a dictionary containing all of the objects attributes and their current values, but I want to do it in a dynamic way (so if later I add another attribute I don’t have to remember to update my function as well).
In php variables can be used as keys, but objects in python are unsuscriptable and if I use the dot notation for this it creates a new attribute with the name of my var, which is not my intent.
Just to make things clearer:
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
Update:
With attributes I mean only the variables of this object, not the methods.
回答 0
假设您有一个诸如
>>> 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__'])
Assuming you have a class such as
>>> class Cls(object):
... foo = 1
... bar = 'hello'
... def func(self):
... return 'call me'
...
>>> obj = Cls()
calling dir
on the object gives you back all the attributes of that object, including python special attributes. Although some object attributes are callable, such as methods.
>>> dir(obj)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'bar', 'foo', 'func']
You can always filter out the special methods by using a list comprehension.
>>> [a for a in dir(obj) if not a.startswith('__')]
['bar', 'foo', 'func']
or if you prefer map/filters.
>>> filter(lambda a: not a.startswith('__'), dir(obj))
['bar', 'foo', 'func']
If you want to filter out the methods, you can use the builtin callable
as a check.
>>> [a for a in dir(obj) if not a.startswith('__') and not callable(getattr(obj, a))]
['bar', 'foo']
You could also inspect the difference between your class and its instance object using.
>>> set(dir(Cls)) - set(dir(object))
set(['__module__', 'bar', 'func', '__dict__', 'foo', '__weakref__'])
回答 1
通常,__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'}
in general put a __iter__
method in your class and iterate through the object attributes or put this mixin class in your class.
class IterMixin(object):
def __iter__(self):
for attr, value in self.__dict__.iteritems():
yield attr, value
Your class:
>>> 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'}
回答 2
正如已经在一些答案/评论中提到的那样,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
)。
As mentioned in some of the answers/comments already, Python objects already store a dictionary of their attributes (methods aren’t included). This can be accessed as __dict__
, but the better way is to use vars
(the output is the same, though). Note that modifying this dictionary will modify the attributes on the instance! This can be useful, but also means you should be careful with how you use this dictionary. Here’s a quick example:
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}
Using dir(a)
is an odd, if not outright bad, approach to this problem. It’s good if you really needed to iterate over all attributes and methods of the class (including the special methods like __init__
). However, this doesn’t seem to be what you want, and even the accepted answer goes about this poorly by applying some brittle filtering to try to remove methods and leave just the attributes; you can see how this would fail for the class A
defined above.
(using __dict__
has been done in a couple of answers, but they all define unnecessary methods instead of using it directly. Only a comment suggests to use vars
).
回答 3
python中的对象将它们的属性(包括函数)存储在称为的字典中__dict__
。您可以(但通常不应该)使用它直接访问属性。如果您只想要一个列表,也可以调用dir(obj)
,它返回带有所有属性名称的Iterable,然后可以将其传递给getattr
。
但是,需要对变量名称做任何事情通常都是不好的设计。为什么不将它们保存在集合中?
class Foo(object):
def __init__(self, **values):
self.special_values = values
然后,您可以使用 for key in obj.special_values:
Objects in python store their atributes (including functions) in a dict called __dict__
. You can (but generally shouldn’t) use this to access the attributes directly. If you just want a list, you can also call dir(obj)
, which returns an iterable with all the attribute names, which you could then pass to getattr
.
However, needing to do anything with the names of the variables is usually bad design. Why not keep them in a collection?
class Foo(object):
def __init__(self, **values):
self.special_values = values
You can then iterate over the keys with for key in obj.special_values:
回答 4
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
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
then just call it as an iterable
s=someclass()
for k,v in s:
print k,"=",v
回答 5
正确的答案是您不应该这样做。如果您想要这种类型的东西,要么只使用dict,要么需要将属性显式添加到某个容器中。您可以通过了解装饰器来实现自动化。
尤其是,顺便说一句,示例中的method1同样具有属性。
The correct answer to this is that you shouldn’t. If you want this type of thing either just use a dict, or you’ll need to explicitly add attributes to some container. You can automate that by learning about decorators.
In particular, by the way, method1 in your example is just as good of an attribute.
回答 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
For 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
对于所有的Python狂热分子,我相信Johan Cleeze会赞成您的教条主义;)。我要离开这个答案,继续贬低它,这实际上使我更加知己。留下你的评论!
对于python 3.6
class SomeClass:
def attr_list1(self, should_print=False):
for k in self.__dict__.keys():
v = self.__dict__.__getitem__(k)
if should_print:
print(f"attr: {k} value: {v}")
def attr_list(self, should_print=False):
b = [(k, v) for k, v in self.__dict__.items()]
if should_print:
[print(f"attr: {a[0]} value: {a[1]}") for a in b]
return b
For all the pythonian zealots out there I’m sure Johan Cleeze would approve of your dogmatism ;). I’m leaving this answer keep demeriting it It actually makes me more confidant. Leave a comment you chickens!
For python 3.6
class SomeClass:
def attr_list1(self, should_print=False):
for k in self.__dict__.keys():
v = self.__dict__.__getitem__(k)
if should_print:
print(f"attr: {k} value: {v}")
def attr_list(self, should_print=False):
b = [(k, v) for k, v in self.__dict__.items()]
if should_print:
[print(f"attr: {a[0]} value: {a[1]}") for a in b]
return b