Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 18th, 2005, 03:45 PM
Nick Jacobson
Guest
 
Posts: n/a
Default 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
  #2  
Old July 18th, 2005, 03:45 PM
Terry Reedy
Guest
 
Posts: n/a
Default Re: Give * operator "deep copy"


"Nick Jacobson" <nicksjacobson@yahoo.com> wrote in message
news:f8097096.0408311154.ae06a5b@posting.google.co m...[color=blue]
> This question is with regard to the * operator as used for sequence
> concatenation.[/color]

* 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:
[color=blue][color=green][color=darkred]
>>> 70*'-' # output separator[/color][/color][/color]
'----------------------------------------------------------------------'[color=blue][color=green][color=darkred]
>>> 20*[0] # initialized vector[/color][/color][/color]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[color=blue]
> There's the well-known gotcha:[/color]

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.
[color=blue]
> a = [[1, 2]]
> b = a*3
> b[0][0] = 4
> print b
>
> Result:
> [[4, 2], [4, 2], [4, 2]][/color]

Because b is a repetition of the mutable content of a as requested.
[color=blue]
> When you wanted:
> [[4, 2], [1, 2], [1, 2]][/color]

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.
[color=blue]
> My question is, since b = a*3 is equivalent to b = a + a + a,[/color]

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.
[color=blue]
> why not use deep copies of a?[/color]

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.' ??
[color=blue]
> That is, let b = a*3 be equivalent to:
> b = copy.deepcopy(a) + copy.deepcopy(a) + copy.deepcopy(a)[/color]

Part of Python's design is to not create/copy objects unless explicitly
requested. And certainly not deepcopies!
[color=blue]
> 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.[/color]

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

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



  #3  
Old July 18th, 2005, 03:47 PM
Raymond Hettinger
Guest
 
Posts: n/a
Default Re: Give * operator "deep copy"

[Terry Reedy][color=blue]
> 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.[/color]

This is an excellent metaphor.

I vote the second line as the QOTW.


Raymond
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles