[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Structure accessible by attribute name or index

Wes Santee

3/17/2010 2:58:00 PM

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.

Cheers,
-Wes
5 Answers

Christian Heimes

3/17/2010 3:15:00 PM

0

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-na...

Bruno Desthuilliers

3/17/2010 3:35:00 PM

0

Wes Santee a écrit :
> 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?

Using the appropriate __magicmethods__ for indexed access and computed
attributes for the attribute access might be a good solution:

# warning : Q&D implementation, would require some sanity checks.

class IndexedValueDescriptor(object):
def __init__(self, index):
self._index = index
def __get__(self, instance, cls):
if instance is None:
return self
return instance[self._index]
def __set__(self, instance, value):
instance[self._index] = value

class Structure(object):
def __init__(self, value1, value2, value3):
self._values = [value1, value2, value3]
def __setitem__(self, index, value):
self._values[index] = value
def __getitem__(self, index):
return self._values[index]
value1 = IndexedValueDescriptor(0)
value2 = IndexedValueDescriptor(1)
value3 = IndexedValueDescriptor(2)




Note that there are probably other solutions... Don't know how
os.stat_result is implemented, might be worth looking at the source
code. But anyway : the above should get you started.

> 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.

Well, that's not really a FAQ AFAICT !-)

Bruno Desthuilliers

3/17/2010 3:36:00 PM

0

Christian Heimes a écrit :
> 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-na...
>

Duh... Should spend some more time reading 2.6's What's New :(

Wes Santee

3/17/2010 4:41:00 PM

0

On Mar 17, 11:14 am, Christian Heimes <li...@cheimes.de> wrote:
> 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-......

Thanks for the pointer. At least I know there is a reason why I
wasn't finding a solution. :)

Wes Santee

3/17/2010 4:55:00 PM

0

On Mar 17, 11:34 am, Bruno Desthuilliers <bruno.
42.desthuilli...@websiteburo.invalid> wrote:
> Wes Santee a écrit :
>
> > 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?
>
> Using the appropriate __magicmethods__ for indexed access and computed
> attributes for the attribute access might be a good solution:

This is as good an excuse as any to get familiar with the rest of the
__magicmethods__. :)

>
> # warning : Q&D implementation, would require some sanity checks.
>
> class IndexedValueDescriptor(object):
>      def __init__(self, index):
>          self._index = index
>      def __get__(self, instance, cls):
>          if instance is None:
>              return self
>          return instance[self._index]
>      def __set__(self, instance, value):
>          instance[self._index] = value
>
> class Structure(object):
>      def __init__(self, value1, value2, value3):
>          self._values = [value1, value2, value3]
>      def __setitem__(self, index, value):
>          self._values[index] = value
>      def __getitem__(self, index):
>          return self._values[index]
>      value1 = IndexedValueDescriptor(0)
>      value2 = IndexedValueDescriptor(1)
>      value3 = IndexedValueDescriptor(2)
>
> Note that there are probably other solutions... Don't know how
> os.stat_result is implemented, might be worth looking at the source
> code. But anyway : the above should get you started.
>
> > 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.
>
> Well, that's not really a FAQ AFAICT !-)