[Tutor] More or less final Chutes & Ladders

spir denis.spir at gmail.com
Mon Jan 6 12:58:10 CET 2014


On 01/05/2014 08:40 PM, Keith Winston wrote:
> On Sun, Jan 5, 2014 at 2:52 AM, Mark Lawrence <breamoreboy at yahoo.co.uk>wrote:
>
>> Homework for you :)  Write a line of code that creates a list of say 3 or
>> 4 integers, then write a line that creates a tuple with the same integers.
>>   Use the dis module to compare the byte code that the two lines of code
>> produce.  The difference is interesting.
>
>
>
> Well... that was a very interesting assignment, though I'm going to have to
> chew on it for a while to understand. I can see that the processing code
> for a tuple is considerably shorter... it is processed in a gulp instead of
> bite by byte... it doesn't have the "Build List" step at all (what goes on
> inside of THAT?)... but I can't claim to really understand what I'm looking
> at.
>
> I notice, for example, if I include only constants (immutable types) in my
> tuple, then it does that gulp thing. If I include a list in there too, all
> hell breaks loose, and suddenly I'm Building Tuples (what goes on inside of
> THAT?). A tuple of tuples still goes down in a single swallow, of course.
>
> Sadly, you can see how my mind works here... hey, this was FUN! You can
> assign me homework any time, teach!

A version of Mark's assigment, with only simple tuple items, but some consts and 
some vars:

from dis import dis

x,y = 1,2

def f (i,j):           # line 5 in source
     a,b = 1,2
     t1 = (1,2)
     t2 = (a,b)
     t3 = (x,y)
     t4 = (i,j)
     l = [1,2]

print(dis(f))

   6           0 LOAD_CONST               3 ((1, 2))
               3 UNPACK_SEQUENCE          2
               6 STORE_FAST               2 (a)
               9 STORE_FAST               3 (b)

   7          12 LOAD_CONST               4 ((1, 2))
              15 STORE_FAST               4 (t1)

   8          18 LOAD_FAST                2 (a)
              21 LOAD_FAST                3 (b)
              24 BUILD_TUPLE              2
              27 STORE_FAST               5 (t2)

   9          30 LOAD_GLOBAL              0 (x)
              33 LOAD_GLOBAL              1 (y)
              36 BUILD_TUPLE              2
              39 STORE_FAST               6 (t3)

  10          42 LOAD_FAST                0 (i)
              45 LOAD_FAST                1 (j)
              48 BUILD_TUPLE              2
              51 STORE_FAST               7 (t4)

  11          54 LOAD_CONST               1 (1)
              57 LOAD_CONST               2 (2)
              60 BUILD_LIST               2
              63 STORE_FAST               8 (l)
              66 LOAD_CONST               0 (None)
              69 RETURN_VALUE

[I call here const a value that is always the same, at every execution; thus in 
principle know not only to the programmer, but to the compiler. I don't mean 
immutable.]

The interesting part for me is the difference of construction when tuple items 
are variable: the build_tuple routine. t2 is also const, abeit so-to-say an 
"implicit" const tuple, while t1 is explicitely const, in value notation itself. 
t2 is variable, since x & y may change in the meantime (between their first def 
and call to f). t3 is variable by so-to-say definition of "variable".

This is a kind of little optimisation in the case a tuple is obviously const. It 
is probably worth it because tuples are fix-size, since they are immutable (they 
may just be fix-size arrays in the backstage, I don't know). Certainly python 
does not even attempt such an optimisation for list due to their mutable and 
flexible-size structure (they are flexible-size, dynamic arrays).

Denis


More information about the Tutor mailing list