Named tuples

Duncan Booth duncan.booth at invalid.invalid
Thu Nov 18 13:58:49 CET 2004


Carlos Ribeiro wrote:

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

Your idea of a __names__ attribute suffers from a problem that the common 
use case would be to return a tuple with appropriate names. Right now you 
can do that easily in one statement but if you have to assign to an 
attribute it becomes messy.

An alternative would be so add a named argument to the tuple constructor so 
we can do:

    	return tuple(('1', '2'), names=('ONE', 'TWO'))

and that would set the __names__ property. If you allow this then you can 
make the __names__ property readonly and the problem of introducing a cycle 
in the __names__ attributes goes away.



More information about the Python-list mailing list