[Python-Dev] list of constants -> tuple of constants

Skip Montanaro skip at pobox.com
Sun Feb 6 17:49:05 CET 2005


In a python-checkins message, Raymond stated:

    Raymond> Replace list of constants with tuples of constants.

I understand the motivation here (the peephole optimizer can convert a tuple
of constants into a single constant that need not be constructed over and
over), but is the effort worth the cost of changing the logical nature of
the data structures used?  If lists are conceptually like vectors or arrays
in other languages and tuples are like C structs or Pascal records, then by
converting from list to tuple form you've somehow muddied the data structure
water just to take advantage of tuples' immutability.

Wouldn't it be better to have the peephole optimizer recognize the throwaway
nature of lists in these contexts:

    for elt in [1, 2, 4, 8, 16]:
        ...

    if foo in [list, tuple]:
        ...

(anywhere a list of constants immediately follows the "in" or "not in"
keywords) and convert them into constants?  The cases you converted all
matched that usage.

Skip



More information about the Python-Dev mailing list