Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #38096
| Newsgroups | comp.lang.python |
|---|---|
| Date | 2013-02-02 21:20 -0800 |
| References | <hKCdnWgrOqkwFpXMnZ2dnUVZ_qadnZ2d@o1.com> |
| Message-ID | <36061bf4-2a65-4ac2-91a1-30f8348f4b0d@googlegroups.com> (permalink) |
| Subject | Re: Please provide a better explanation of tuples and dictionaries |
| From | Rick Johnson <rantingrickjohnson@gmail.com> |
############################################################
# 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 items written 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!
Now you may ask yourself:
WHAT THE HELL IS A TUPLE? AND WHERE DID THIS ESOTERIC TERM ORIGINATE!
And if you ask the oracle (aka: Google) you might get this answer:
*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 names fail 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*
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Please provide a better explanation of tuples and dictionaries "Daniel W. Rouse Jr." <dwrousejr@nethere.comNOSPAM> - 2013-01-29 18:55 -0800
Re: Please provide a better explanation of tuples and dictionaries Chris Angelico <rosuav@gmail.com> - 2013-01-30 14:11 +1100
Re: Please provide a better explanation of tuples and dictionaries "Daniel W. Rouse Jr." <dwrousejr@nethere.comNOSPAM> - 2013-01-29 19:42 -0800
Re: Please provide a better explanation of tuples and dictionaries Chris Angelico <rosuav@gmail.com> - 2013-01-30 14:51 +1100
Re: Please provide a better explanation of tuples and dictionaries Mitya Sirenef <msirenef@lightbird.net> - 2013-01-29 22:51 -0500
Re: Please provide a better explanation of tuples and dictionaries John Gordon <gordon@panix.com> - 2013-01-30 05:15 +0000
Re: Please provide a better explanation of tuples and dictionaries "Daniel W. Rouse Jr." <dwrousejr@nethere.comNOSPAM> - 2013-01-29 22:14 -0800
Re: Please provide a better explanation of tuples and dictionaries Chris Angelico <rosuav@gmail.com> - 2013-01-30 17:25 +1100
Re: Please provide a better explanation of tuples and dictionaries Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-01-30 07:12 +0000
Re: Please provide a better explanation of tuples and dictionaries rusi <rustompmody@gmail.com> - 2013-01-30 00:14 -0800
Re: Please provide a better explanation of tuples and dictionaries Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-02 21:20 -0800
Re: Please provide a better explanation of tuples and dictionaries Michael Torrie <torriem@gmail.com> - 2013-02-03 00:14 -0700
Re: Please provide a better explanation of tuples and dictionaries Chris Angelico <rosuav@gmail.com> - 2013-02-03 18:21 +1100
csiph-web