tuple.index()

Roy Smith roy at panix.com
Sun Dec 17 10:18:53 EST 2006


greg <greg at cosc.canterbury.ac.nz> wrote:
> A Python tuple is like a C struct, and a Python list is like a C array.

A Python list is more like C++/STL vector than an array, but that's 
probably picking nits.

The real problem is that while the *intent* of the Python tuple is to act 
like a C/C++ struct (again, the C++ flavor is a better analogy, since 
tuples have methods), that's a poor comparison.

The struct does lookup by name, the tuple is inherently index based.  Yes, 
I know that the struct element names are converted to offsets by the 
compiler, but from the programming interface point of view, they're named 
elements.  In that respect, a Python dict is a better analogy for a C++ 
struct than a tuple is.

Also, consider that tuples and lists are completely fungible.  For any list 
l, list(tuple(l)) is an identity operation.  The same is true of 
tuple(list(t)) for any tuple t.  That implies that neither one contains any 
information that the other one doesn't.



More information about the Python-list mailing list