Structure accessible by attribute name or index

Christian Heimes lists at cheimes.de
Wed Mar 17 11:14:38 EDT 2010


Wes Santee wrote:
> I am very new to Python, and trying to figure out how to create an
> object that has values that are accessible either by attribute name,
> or by index.  For example, the way os.stat() returns a stat_result or
> pwd.getpwnam() returns a struct_passwd.
> 
> In trying to figure it out, I've only come across C implementations of
> the above types.  Nothing specifically in Python.  What is the Python
> native way to create this kind of object?
> 
> I apologize if this has been widely covered already.  In searching for
> an answer, I must be missing some fundamental concept that is
> excluding me from finding an answer.

You can't use the same implementation as the result object of os.stat()
and others. However Python 2.6 has a new factory that creates a similar
datatype called named tuple:
http://docs.python.org/library/collections.html#namedtuple-factory-function-for-tuples-with-named-fields




More information about the Python-list mailing list