Lists and Tuples

Michael T. Babcock mbabcock at fibrespeed.net
Fri Dec 5 15:14:11 EST 2003


>
>
>That's true, but another answer is: you should use tuples for short 
>>sequences of diverse items (like the arguments to a function).  You 
>>should use lists for longer sequences of similar items.
>>
>  
>
>I'm curious what you're getting at. I.e., what does diversity or
>similarity have to do with the choice? Is that an aesthetic thing?
>(In which case 'should' should be qualified a bit, IWT 
>Or what am I missing?
>

I think what he may be getting at is that in the case of a list, you 
should be able to perform the same operation(s) on all members.  I'm not 
sure this is a should or not, but its true in my behaviours as a 
programmer at least.

def runquery(query):
    try:
        # do stuff
    except query_error, e:
       errors += e
    result_count = len(results)
    return (result_count, results, errors)

(count, results, errors) = runquery( ... )

... but then results would probably be a list of rows ...

results = [
    { 'id': 1, 'name': 'Michael', 'position': 'Hacker' },
    { 'id': 2, 'name': 'Dave', 'position': 'Boss' }
    ]

Each item in the list does not have to be the same, but I usually assume 
that a list of 'somethings' is homogenous.  In this case, all items are 
result-sets from the database.  I expect nothing from a tuple ;-).

Does this make sense?

-- 
Michael T. Babcock
C.T.O., FibreSpeed Ltd.
http://www.fibrespeed.net/~mbabcock







More information about the Python-list mailing list