Give * operator "deep copy"

Terry Reedy tjreedy at udel.edu
Tue Aug 31 20:54:52 EDT 2004


"Nick Jacobson" <nicksjacobson at yahoo.com> wrote in message 
news:f8097096.0408311154.ae06a5b at posting.google.com...
> This question is with regard to the * operator as used for sequence
> concatenation.

* is specifically a repetition operator.  And as you wrote, n*seq is a 
sequence operation and not specifically a list operation.  Any sensible 
discussion of changing its semantics, even though completely hypothetical 
(since this will not happen for the foreseeable future), must be in the 
context of sequences, and not just lists.

The purpose of n*seq is to compactly specify a repetitious sequence and do 
so in a way such that the interpreter can efficiently create the requested 
result.  Here are two realistic examples:

>>> 70*'-' # output separator
'----------------------------------------------------------------------'
>>> 20*[0] # initialized vector
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

> There's the well-known gotcha:

The gotcha arises in the context of 'multiplying' a sequence containing a 
mutable sequence.  It gotcha arises from the conjunction of three factors:
1. the desire to create a 2D structure with a shortcut operation intended 
to construct 1D sequences.
2. the mistake of ignoring identity as an important property for mutable 
objects and as a part of the definition of 'repetitious' for sequences of 
such objects
3. the mistake of taking 'container' too literally.  Tuples and lists are 
like rosters, not rooms.  A student can only be in one room (at a time) but 
can simultaneously be on the roster of multiples classes and clubs.  If, 
for example, a student gets an honor, then every class and club 
'containing' that student now 'contains' an honored member.  What rosters 
actually contain are some sort of identifier for each student, not the 
students themselves.

> a = [[1, 2]]
> b = a*3
> b[0][0] = 4
> print b
>
> Result:
> [[4, 2], [4, 2], [4, 2]]

Because b is a repetition of the mutable content of a as requested.

> When you wanted:
> [[4, 2], [1, 2], [1, 2]]

If one does not want object repetition (as opposed to value repetition), 
then one should not use the object repetition operator.  Use a loop to 
create different lists instead.

> My question is, since b = a*3 is equivalent to b = a + a + a,

The equivalence is quite limited.  'n*a' is valid code.  'a+a+...+a n 
times' is not, so 'n*a' is result equivalent instead to an explicit loop or 
list comprehension.  Furthermore, the sequential addition falls into the 
O(n*n) runtime trap, which the O(n) repetition operator is given to us to 
avoid.

> why not use deep copies of a?

You seem to be saying 'since multiplication is a handy and speedy shorthand 
for repeated addition, let us change it to mean to something else.' ??

> That is, let b = a*3 be equivalent to:
> b = copy.deepcopy(a) + copy.deepcopy(a) + copy.deepcopy(a)

Part of Python's design is to not create/copy objects unless explicitly 
requested.  And certainly not deepcopies!

> It seems much more likely that someone would want to create copies of
> an item, rather than inserting the same item several times into the
> list.

For characters in a string, the distinction is not especially meaningful. 
For strings (in a non-string sequence), tuples, numbers, and other 
non-mutables, copies are a waste of time and space and reuse of one is 
exactly what one should want!  Again, * is not just for lists, either as 
container or containee.

> If this has been previously discussed/documented, please point me to
> where I can read about it.

The meaning of n*seq is documented in the ref manual.  The buggy use of it 
comes up on the newsgroup fairly often.  Try googling the group archives.

Terry J. Reedy






More information about the Python-list mailing list