How to force a single number to be a tuple

Roy Smith roy at panix.com
Fri Aug 6 18:15:13 EDT 2004


bryanjugglercryptographer at yahoo.com (Bryan Olson) wrote:
> Python tuples overlap too much with lists, and differ from the 
> functional/relational view in which a tuple is an element of the 
> Cartesian product of zero or more domains.

For those of us who went to school a while ago, and perhaps didn't pay 
as much attention in math class as we should have, could you translate 
"an element of the Cartesian product of zero or more domains" into 
English?

> What Python calls 'tuples' are really immutable lists.

That's the way I've always thought of them.  I know the cognoscenti will 
insist that tuples are anonymous structures of heterogeneous types and 
lists are ordered collections of homogenous data, but I don't buy the 
distinction.  There's nothing in the language that makes me think homo 
vs. hetero for either.  Maybe I'm being obstinate (my wife has certainly 
accused me of that on one more than one occasion), or maybe I just swing 
both ways when it comes to data containers, but that's the way I see it.

I remember once having a white-board discussion with some C++ friends of 
mine where we were talking about writing code to parse things like:

insert into foo values (1, 2, "three", "four");

My Python code built up a list of the values and generated [1, 2, 
"three", "four"].  My two friends recoiled violently at the idea that I 
would put heterogeneous data types into a list.  I passed it off as 
simply being due to their poor unfortunate upbringing in the C++/STL 
world of type bondage, while I was living in the carefree bohemian 
Python world.  I was shocked to discover some time later that Python was 
not as bohemian as I thought, and the priests and elders would have been 
as dismayed at my carefree mixing of data types in a list as my stodgy 
C++ brethren were.

I personally think tuples should have used <> instead of ().  It would 
have resolved a lot of notational ambiguity.



More information about the Python-list mailing list