Reputation: 5345
I have a Python object called "form" looking like this:
> dir(form)
['Meta', 'SECRET_KEY', 'TIME_LIMIT', '__class__', '__contains__', '__delattr__', '__delitem__', '__dict__', '__doc__', '__format__', '__getattribute__', '__getitem__', '__hash__', '__init__', '__iter__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_errors', '_fields', '_get_translations', '_prefix', '_unbound_fields', '_wtforms_meta', 'csrf_enabled', 'csrf_token', 'data', 'errors', 'f', 'generate_csrf_token', 'hidden_tag', 'i', 'is_submitted', 'meta', 'node_1', 'node_2', 'node_3', 'node_4', 'nodes', 'populate_obj', 'process', 'r', 'reader', 'submit', 'validate', 'validate_csrf_data', 'validate_csrf_token', 'validate_on_submit']
This object always contains a given number of objects which names start with "node_". The number of these objects is alway different.
I need to retreive all of these objects from another function, and get the content of form.node_1.data, form.node_2.data, form.node_3.data, etc.
I can list all these "node" objects like this:
for u in dir(form):
if not u.startswith('__'):
if u.startswith('node_'):
print u
But all I get is a string, not the object itself.
How can I access the value of all these "node objects"?
(I hope the vocabulary is right...)
Upvotes: 2
Views: 80
Reputation: 4837
You can grab those pretty easy:
atribs = [getattr(form, attr) for attr in dir(form) if attr.startswith('node_')]
Upvotes: 1
Reputation: 3674
Try getattr
:
for u in dir(form):
if not u.startswith('__'):
if u.startswith('node_'):
print getattr(form, u)
Upvotes: 2