immutable count and index

Skip Montanaro skip at pobox.com
Mon Aug 13 14:29:10 EDT 2001


    Grant> But maybe lists and tuples are so different under-the-hood that
    Grant> it would be pure duplication (as I assume is currently the case
    Grant> for count/index on lists/strings.)

There really isn't much different under-the-hood.  Tuples can make
immutability assumptions in some places that lists can't, but most of the
code where they have common functionality they could probably be very much
alike.  The speed-tradeoff macros for setting and getting stuff are
identical.

    /* Macro, trading safety for speed */
    #define PyTuple_GET_ITEM(op, i) (((PyTupleObject *)(op))->ob_item[i])
    #define PyTuple_GET_SIZE(op)    (((PyTupleObject *)(op))->ob_size)

    /* Macro, *only* to be used to fill in brand new tuples */
    #define PyTuple_SET_ITEM(op, i, v) (((PyTupleObject *)(op))->ob_item[i] = v)

    ...

    /* Macro, trading safety for speed */
    #define PyList_GET_ITEM(op, i) (((PyListObject *)(op))->ob_item[i])
    #define PyList_SET_ITEM(op, i, v) (((PyListObject *)(op))->ob_item[i] = (v))
    #define PyList_GET_SIZE(op)    (((PyListObject *)(op))->ob_size)

Note the comment for PyTuple_SET_ITEM.

-- 
Skip Montanaro (skip at pobox.com)
http://www.mojam.com/
http://www.musi-cal.com/




More information about the Python-list mailing list