except clause syntax question

Ethan Furman ethan at stoneleaf.us
Tue Jan 31 11:56:57 EST 2012


Charles Yeomans wrote:
> On Jan 31, 2012, at 9:51 AM, Steven D'Aprano wrote:
>> On Tue, 31 Jan 2012 08:57:31 -0500, Charles Yeomans wrote:
>>
>>> I don't think of a tuple as a container, and I don't think it a
>>> misunderstanding on my part to think this.
 >>
>> Well, it is a misunderstanding, because tuples ARE containers. You
 >> might as well say "I don't think of boxes as containers". What
 >> exactly are they if not containers?
> 
> Tuple is a heterogenous datatype that allows one to define objects
 > ad hoc.

And any object can be seen as a container for its component pieces -- 
some are just more general than others.

Compare:

location = (13, 4, 9)            # line, word, char
time = (10, 15, 41)              # hour, minute, second
result = ('this', 'that', 'huh') # result a, result b, result c

with:

record1 = Record('Ethan', 41, Male)
record2 = Record('Charles', 37, Male)
record3 = Record('Steven', 43, Male)
record4 = Record('Jennifer', 39, Female)

In this example, Records have a set layout and so it is more common to 
think of a Record as a thing;  location, time, and result, however, are 
all random tuples created on the fly with no absolute restrictions on 
what goes in which position.

lists, dicts, sets, and tuples are general purpose containers; strs (and 
most user defined classes) are special purpose containers.

> That is to say, a tuple represents a single thing distinct from its
 > components.

You could say that about a list as well.  Doesn't change the fact that a 
list is a container.

> One can certainly view a tuple as a list, just as one can view a string
 > as a list of characters, and sometimes that's useful; the Python dictum
 > "there should only be one way to do it" doesn't imply that there is only
 > one way to think of it.

The 'dictum' is "there should only be one *obvious* way to do it" 
(emphasis added).

~Ethan~



More information about the Python-list mailing list