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

Guido van Rossum gvanrossum at gmail.com
Sun Feb 6 17:54:58 CET 2005

On Sun, 6 Feb 2005 10:49:05 -0600, Skip Montanaro <skip at pobox.com> wrote:
> 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.

I'm with Skip, *unless* the change is in a PROVEN TIME-CRITICAL PIECE OF CODE.

Let's not hand-micro-optimize code just because we can.

--Guido van Rossum (home page: http://www.python.org/~guido/)

More information about the Python-Dev mailing list