[Tutor] Creating a complex python object

Jeff Shannon jeff at ccvcorp.com
Sat Aug 14 01:26:59 CEST 2004


nik wrote:

>> At 12:13 PM 8/13/2004, Jeff Shannon wrote:

>>> The common usage, now, is that lists usually contain a sequence of 
>>> similar things, whereas tuples are usually more like a record, 
>>> grouping together dissimilar things that are conceptually related. 
>>> Thus, for example, in the date-time tuple returned by time.gmtime() 
>>> each position within the tuple represents a different thing.  In 
>>> contrast, each position in a list represents a different instance, 
>>> but all of the contents are the same type of thing.  This distinction 
>>> is just convention, of course, and there's nothing forcing you to 
>>> follow it, but you'll find that just about every standard library 
>>> module (and most other code, as well) will follow this guideline.
>>
> I've seen the homogeneous vs heterogeneous description of lists and 
> tuples, but pure convention doesn't seem to be a very good argument for 
> it. There must be a good practical reason that it's settled in that form 
> - why didn't it settle in the opposite fashion; lists=heterogeneous, 
> tuples=homogeneous? Is it the immutability of the tuple, or the fact you 
> can't use a list as a key that's pushed it in that direction, or was it 
> really as simple as one guy in the early days saying 'I prefer this 
> convention'?

I suspect that the immutable nature of tuples makes them seem a little 
more... distinct.  Consider the case where some fields of a record are 
empty.  Because lists can vary in length, you're not used to the 
thought of "empty" slots in a list; in contrast, tuples (once created) 
don't change in size, so they seem more natural as something with 
"empty" slots.  (Of course, this may be a side effect of my already 
thinking of tuples as records, rather than a cause, but it sounds good 
to me. ;) )

Jeff Shannon
Technician/Programmer
Credit International





More information about the Tutor mailing list