Making an object's attributes iterable

2020-06-27 15:19发布

问题:

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.

回答1:

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


回答2:

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:

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.



回答4:

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.