Re: Please provide a better explanation of tuples and dictionaries

Discussion in 'Python' started by Rick Johnson, Feb 3, 2013.

1. Rick JohnsonGuest

############################################################
# Quote: Daniel Rouse Jr. #
############################################################
# To me, this looks like an array. Is tuple just the #
# Python name for an array? #
############################################################

The problem with understanding Python collections is directly due to improper naming.

First let's consider the sequence types "list" and "tuple". For the most part lists and tuples are exactly the same. They are both containers for holding values.

GvR wisely choose to borrow the English word "list" over the esoteric CS term "array", but he /unwisely/ choose to borrow the maths term "tuple" over something more self-documenting to describe what is basically an immutable list.

Even someone who has no programming experience could most probably intuit what a "Python list" /is/. Everyone has made a grocery list, or a "to-do" list. The transformation from a tangible object like: *a linear list of itemswritten on paper* to an intangible object like: *a Python list holding N objects* is not very difficult fathom. HOWEVER, then along comes the "seemingly" innocent tuple with fiery red hair and that devilish little grin intent on screwing up the whole logical flea circus!

WHAT THE HELL IS A TUPLE? AND WHERE DID THIS ESOTERIC TERM ORIGINATE!

*Google Said:* /"""In mathematics and computer science, a tuple is an ordered list of elements. In set theory, an (ordered) n-tuple is a sequence (or ordered list) of n elements, where n is a non-negative integer. """"/

Okay google, so a tuple is an /ordered set/ and a list is an /ordered collection/, got it, (and thanks GvR for the mental overload!) however the namesfail to convey this VERY important piece of information

For the fix, it would seem logical to simply extend the term "list" in a manner that will convey a "set" relationship. "DynamicList" and "StaticList" fit the bill HOWEVER these terms are FAR to verbose to use on a daily basis! Now, we could naively use "list" for an ordered collection, and "staticlist" for an ordered set, HOWEVER even this is a foolish choice!

The final solution is NOT two different types with verbose names, NO, the solution is ONE ordered collection type with a method to convert it into an ordered set. Observe:

py> list = list()
py> list.extend([1,2,3])
[1,2,3]
py> list.append('logical')
[1,2,3,'logical']
py> staticList = list.freeze()
py> staticList[-1]
'logical'
py> staticList.append('error')
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
staticList.append('error')
AttributeError: 'StaticList' object has no attribute 'append'

*school-bell*
Rick Johnson, Feb 3, 2013

2. Michael TorrieGuest

On 02/02/2013 10:20 PM, Rick Johnson wrote:
> *school-bell*

I'm already regretting typing this, but really? The term, "tuple," was
used rather consistently all through my university years. And Python's
use of it is consistent with how it is used all through computer
science. And for that matter it is consistent with how other languages,
including LISP(!), use the term. As you say, we borrow it from Math.
And for good reason.

Now I know they say that foolish consistency is the hobgoblin of little
minds, but using the word, "tuple," in this consistent way is hardly
foolish. In this case, as is usually the case with your strange
criticisms of well-established practice, there are good reasons for it.

TL;DR: I find your list freezing proposal to be needlessly complicated.
No the burden of proof is not on me to explain why tuples are so.
Michael Torrie, Feb 3, 2013

3. Chris AngelicoGuest

On Sun, Feb 3, 2013 at 6:14 PM, Michael Torrie <> wrote:
> TL;DR: I find your list freezing proposal to be needlessly complicated.
> No the burden of proof is not on me to explain why tuples are so.

We have a list-freezing mechanism already.

>>> list=[1,2,3,'logical']
>>> staticList=tuple(list)

Et voila! Looks fine to me.

ChrisA
Chris Angelico, Feb 3, 2013