[Tutor] Use iterator to refer to an object's attribute?
Kent Johnson
kent37 at tds.net
Thu Apr 20 13:44:25 CEST 2006
Ron Britton wrote:
> Short version: How do I use an iterator to refer to an object's
> attribute? E.g., "z" is a list of attributes of "b":
> for x, y in z:
> for a in b.x.y
getattr(b, 'foo') is the same as b.foo. getattr takes a string for the
name of the attribute. So you would need
getattr(getattr(b, x), y)
>
> Also, any suggestions for my overall structure below?
For once I agree with Alan on a design question :-)
You can define a useful structure directly in your configuration. It
could be nested lists and dictionaries or nested class instances,
whichever works better for the application. I think I would use nested
class instances. This is one of Python's great strengths - that you can
easily declare complex data structures in code.
PS I suggest you call your data structure something other than
'database' since that word has a fairly specific meaning.
Kent
More information about the Tutor
mailing list