An oddity in list comparison and element assignment

Terry Reedy tjreedy at udel.edu
Fri Jun 2 21:56:22 EDT 2006


<michael.f.ellis at gmail.com> wrote in message 
news:1149277242.199765.213600 at g10g2000cwb.googlegroups.com...
> (As an aside, may I point out that Python In A Nutshell states on page
> 46 "The result of S*n or n*S is the concatenation of n copies of S".

It would be more exact to say that S*n is [] extended with S  n times, 
which makes it clear that 0*S == S*0 == [] and which avoids the apparently 
misleading word 'copy'.  I presume the C implementation is the equivalent 
of

def __mul__(inlist, times):
    result = []
    for i in range(times):
        result.extend(inlist)
    return result

Or one could say that the result *is the same as* (not *is*) the 
concatenation of n *shallow* copies of S.  'Shallow' means that each copy 
of S would have the same *content* (at the id level) as S, so that the 
result would contain the content of S n times, which is to say, when len(S) 
== 1, n slots with each slot bound to the *identical* content of S.

When the content is immutable, the identicaliy does not matter.  When it 
*is* mutable, it does.

> It  might be well to put a warning in a future edition that this is not
> strictly the case.)

Did you mean anything else by 'not strictly the case'?

Terry Jan Reedy







More information about the Python-list mailing list