Named tuples

Carlos Ribeiro carribeiro at gmail.com
Thu Nov 18 07:02:00 EST 2004


On Thu, 18 Nov 2004 05:04:16 GMT, Bryan <belred1 at yahoo.com> wrote:
> Bengt Richter wrote:
> 
> 
> > On Thu, 18 Nov 2004 02:57:23 GMT, Bryan <belred1 at yahoo.com> wrote:
> > [...]
> >
> >>it should be possible to avoid a recusive problem:
> >>
> >>
> >>>>>a = ('1', '2')
> >>>>>a.__names__ = ('ONE', 'TWO')
> >
> > I guess you are working with a patched version of python?
> >
> >  >>> a = ('1', '2')
> >  >>> a.__names__ = ('ONE', 'TWO')
> >  Traceback (most recent call last):
> >    File "<stdin>", line 1, in ?
> >  TypeError: 'tuple' object has only read-only attributes (assign to .__names__)
> >
> > Regards,
> > Bengt Richter
> 
> we were discussing a feature that doesn't yet exist.  i believe it's possible to
> get around the recursion issue by only allowing the __names__ attribute to be
> accessed in the immediate explicitly called tuple.  i really do like the idea of
> named tuple elements.


There are a few requirements that can be imposed to avoid problems.
First, __names__ is clearly a property, acessed via get & set (which
allows to trap some errors). It should accept only tuples as an
argument (not lists) to avoid potential problems with external
references and mutability of the names. As for the validation, I'm not
sure if it's a good idea to check for strings. maybe just check if the
'names' stored in the tuple are immutable (or perhaps 'hashable') is
enough.

-- 
Carlos Ribeiro
Consultoria em Projetos
blog: http://rascunhosrotos.blogspot.com
blog: http://pythonnotes.blogspot.com
mail: carribeiro at gmail.com
mail: carribeiro at yahoo.com



More information about the Python-list mailing list