To Tuple or to Map?

Jason Orendorff jason at jorendorff.com
Thu Jan 24 13:37:46 EST 2002


> (a) Does a map have significantly more run-time
>     overhead than a tuple?

Yes.  Probably around 5x for your case.

> (b) Is there any way to mark a variable (nColName)
>     as a constant so that the "byte compiler" can
>     resolve the value to an integer (instead of
>     requiring an access to the local namespace)

No.

> (c) Which technique would you use?

Tuples.

But I might consider using tuples only within one layer,
and allowing users from outside that layer to play with
objects:

# intended to be private to my_database.py
_my_table = {
      12 : (12, 'Jason', 'xyz at zy.com', 147, 14475)
      13 : (13, 'Tim', 'foo at bar.com', 12, 11992)
      ...
    }

# for public use
class Person:
    def __init__(self, data):
        self.id, self.name, self.email, self.age, self.acct_num = data

# for public use
def lookup_person(id):
    return Person(_my_table[id])

## Jason Orendorff    http://www.jorendorff.com/




More information about the Python-list mailing list