473,378 Members | 1,441 Online

# 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

Thanks!

--Nick
Jul 18 '05 #1
2 2217

"Nick Jacobson" <ni***********@yahoo.com> wrote in message
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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

### Similar topics

 0 by: Kevin Schneider | last post by: Please forgive me if this is a bit off topic, but I haven't had any takers in other forums. With ASP.NET projects, is it possible to have VS.NET automatically change the configuration options of... 27 by: Shagy | last post by: Greetings, I've been trying to find an equivant c funtion to the c++ copy function. Description: copy(char *cstring, size_t count, size_t offset); Copies "count" characters from a C-style... 1 by: Alfonso Morra | last post by: Hi, I have the ff data types : typedef enum { VAL_LONG , VAL_DOUBLE , VAL_STRING , VAL_DATASET }ValueTypeEnum ; 1 by: Jesper Denmark | last post by: Hi, Is deep serialization possible using XML. Know any good tutorials?. By deep serialization I mean the ability that an object being serialized automatically serialize its members. E.g ... 1 by: Knepper, Michelle | last post by: Hi out there, I'm a first-time user of the "Copy ... From..." command, and I'm trying to load a table from a text flat file. http://www.postgresql.org/docs/7.4/static/sql-copy.html I don't... 4 by: lars.uffmann | last post by: Hey everyone! I am (still) working on a project that I took over from former students, so don't blame me for the criminal approach on coding *g* The problem I have is fairly easy and while I... 40 by: Mark P | last post by: I'm implementing an algorithm and the computational flow is a somewhat deep. That is, fcn A makes many calls to fcn B which makes many calls to fcn C, and so on. The return value of the outermost... 15 by: Matt Kruse | last post by: Consider the following two functions: function func1() { var o ; if ( (o=document.forms) && (o=o) && (o=o.elements) && (o=o.sel) && (o=o.options) && (o=o) 3 by: Jess | last post by: Hello, The C++ reference says the return result of "copy" is an output iterator. I'm wondering how I can assign the returned iterator to some other iterator. I tried int main(){ string... 0 by: Faith0G | last post by: I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be... 0 by: ryjfgjl | last post by: In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import... 0 by: taylorcarr | last post by: A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,... 0 by: Charles Arthur | last post by: How do i turn on java script on a villaon, callus and itel keypad mobile phone 0 by: aa123db | last post by: Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function \$name\$ (\$parameters\$) { } ... 0 by: ryjfgjl | last post by: In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files... 0 by: emmanuelkatto | last post by: Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel 1 by: nemocccc | last post by: hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions? 0 by: Hystou | last post by: There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...