471,602 Members | 1,309 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,602 software developers and data experts.

Give * operator "deep copy"

This question is with regard to the * operator as used for sequence
concatenation.

There's the well-known gotcha:

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

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

My question is, since b = a*3 is equivalent to b = a + a + a, why not
use deep copies of a? That is, let b = a*3 be equivalent to:

b = copy.deepcopy(a) + copy.deepcopy(a) + copy.deepcopy(a)

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.
If this has been previously discussed/documented, please point me to
where I can read about it.

Thanks!

--Nick
Jul 18 '05 #1
2 2120

"Nick Jacobson" <ni***********@yahoo.com> wrote in message
news:f8*************************@posting.google.co m...
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

Jul 18 '05 #2
[Terry Reedy]
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.


This is an excellent metaphor.

I vote the second line as the QOTW.
Raymond
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

27 posts views Thread by Shagy | last post: by
1 post views Thread by Alfonso Morra | last post: by
1 post views Thread by Jesper Denmark | last post: by
40 posts views Thread by Mark P | last post: by
15 posts views Thread by Matt Kruse | last post: by
3 posts views Thread by Jess | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by MichaelMortimer | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.