Making an object's attributes iterable

2020-06-27 15:00发布

I'm getting returned a list with objects that have multiple attributes like so:

results = q.fetch(5)
for p in results:
    print "%s %s, %d inches tall" % (p.first_name, p.last_name, p.height

Is it possible to iterate over these attributes so I can do something like for x in p. I want to check the value of each one, but I don't want to create a huge block of IF statements.

4条回答
Lonely孤独者°
2楼-- · 2020-06-27 15:04

You can subclass the original variable type, and define your own cunning iter(self) function, to get what you want. e.g. to change the way a dictionary iterates:-

>>> class mydict(dict):
...    def __iter__(self):
...      for i in self.items():
...          yield i
... 
>>> x = mydict( {'a' : 1, 'b':2 } )
>>> for i in x:
...   print i
... 
('a', 1)
('b', 2)
查看更多
劫难
3楼-- · 2020-06-27 15:19

I warn against doing this. There are rare exceptions where it's warranted, but almost all the time it's better avoiding this sort of hackish solution. If you want to though, you could use vars() to get a dictionary of attributes and iterate through it. As @Nick points out below, App Engine uses properties instead of values to define its members so you have to use getattr() to get their values.

results = q.fetch(5)
for p in results:
    for attribute in vars(p).keys()
        print '%s = %s' % (attribute, str(getattr(p, attribute)))

Demonstration of what vars() does:

>>> class A:
...     def __init__(self, a, b):
...         self.a = a
...         self.b = b
... 
>>> a = A(1, 2)
>>> vars(a)
{'a': 1, 'b': 2}
>>> for attribute in vars(a).keys():
...     print '%s = %s' % (attribute, str(getattr(a, attribute)))
... 
a = 1
b = 2
查看更多
祖国的老花朵
4楼-- · 2020-06-27 15:24

To get a list of properties on a model class, call Model.properties() (or instance.properties() - it's a class method). This returns a dictionary mapping property names to Property class instances; you can fetch the value of the properties by doing getattr(instance, name).

If you're using Expando, there's also instance.dynamic_properties(), which returns a list of dynamically defined properties on that object.

查看更多
一夜七次
5楼-- · 2020-06-27 15:28

With the assumption that the object you get back from q.fetch(5) having a __dict__ attribute, you can simply use pprint to display your information.

>>> import pprint
>>> results = q.fetch(5)
>>> pprint.pprint(results.__dict__())

Or alternatively, if it has something that can be converted to a dictionary, a similar notation would work

>>> pprint.pprint(dict(results.dict_like_property))

I would suggest though, that this isn't a good approach to take, but it does hold for debugging code easily.

查看更多
登录 后发表回答