Questions tagged [shallow-copy]

A shallow copy of an object is a duplicate that may not be fully independent of the original, in the sense that any references / pointers it holds to other objects refer to the same objects that the original's do. Use this tag for questions regarding implementing or using shallow copying methods.

Shallow copying is a process by which an object is duplicated to create a copy that may not be wholly independent of the original. Also known as "field by field" copying, this differs from deep copying with respect to the treatment of references / pointers to other objects: shallow copying copies the references / pointers held by the original, whereas deep copying makes a deep copy of each referenced object, too. Shallow copying is "shallow" in the sense that it copies only the original object itself, not any appearing deeper in its reference graph.

Related tags

Links

394 questions
2
votes
1 answer

Dictionary copy() - is shallow deep sometimes?

According to the official docs the dictionary copy is shallow, i.e. it returns a new dictionary that contains the same key-value pairs: dict1 = {1: "a", 2: "b", 3: "c"} dict1_alias = dict1 dict1_shallow_copy = dict1.copy() My understanding is that…
boardtc
  • 1,304
  • 1
  • 15
  • 20
2
votes
3 answers

Pass by value confusion in Scheme

Consider the following procedure taken from SICP: (define (make-withdraw balance) (lambda (amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) …
lightning_missile
  • 2,821
  • 5
  • 30
  • 58
2
votes
0 answers

Why a hard-coding switch performs better at restricted shallow copying?

I'm working on a react custom renderer, which I have to shallow copy react props to the backend element, which there are reserved properties like "key", "ref", "children" I should filter out, especially the "children" conflicts with element's…
2
votes
1 answer

fill an array with class instances

I'm get stuck filling up an array of class instances. To make a long story very short I create a class person (with attributes and functions on it) and I'd like to fill up an array of person's instances just pushing in the array "new" instance of…
2
votes
3 answers

What are problems with shallow copy?

This is an interview question I saw from here: http://www.careercup.com/question?id=1707701 Want to know more about this .thanks
Josh Morrison
  • 7,488
  • 25
  • 67
  • 86
2
votes
1 answer

Implementing a copy method of a class implementing an interface - Java

I have this weird situation, where I have a class TheClass implementing interface TheInterface and class TheClass is supposed to have a copy() method with return type TheInterface, and it should make a shallow copy of itself. When trying to call my…
petex7
  • 23
  • 3
2
votes
3 answers

Shallow copy in 1-D list vs 2-D list

I found lots of discussions related to "shallow copy" in Python, but I cannot find my exact issue. As per my understanding, creating a shallow copy still contains references to the original values of the list. This holds true in following case of a…
itssubas
  • 163
  • 2
  • 11
2
votes
1 answer

Understanding Deep vs Shallow Copy in Python 2.x

I was looking online and I came across these 3 segments of code. The question is to predict the output and explain why. Example 1: x = 42 y = x x = x + 1 print x print y Output Example 1: 43 42 Example 2: x = [1, 2, 3] y = x x[0] = 4 print…
Ely Fialkoff
  • 642
  • 1
  • 5
  • 12
2
votes
3 answers

Is it possible to shallow copy a singleton class object?

Using the clone method, can we get many instances of a class which has been made singleton ? Also, is it necessary to write "implements Cloneable" because I learnt that all objects extend from Object class and hence the child object calling…
Prabhat Gaur
  • 146
  • 1
  • 10
2
votes
3 answers

Changing list elements in shallow copy

I have one question about list shallow copy. In both examples, I modified one element of the list, but in example 1, list b changed, while in example 2, list d is not changed. I am confused since in both examples, I modified an element of the list.…
tennisboy
  • 121
  • 3
2
votes
2 answers

Shallow copy of an object in an intent in android

I have a few objects I want to pass to other activities through intents. However, they only need to be shallow copies of the other object, as they are only going to be read (and even if they were going to be modified, I would want them modified in…
Leif Andersen
  • 21,580
  • 20
  • 67
  • 100
2
votes
1 answer

What are the implications of performing a shallow copy on an array in order to resize it?

If my understanding of deep and shallow copying is correct my question is an impossible one. If you have an array (a[10]) and perform a shallow copy (b[20]) wouldn't this be impossible as the data in b wouldn't be contiguous? If i've got this…
Adam Naylor
  • 6,172
  • 10
  • 49
  • 69
2
votes
4 answers

trying to do a shallow copy on list in python

So i copied using copy.copy which is shallow but if i change the value in one the value in the other won't get changed. import copy a=[1,2,3,4,5] b=copy.copy(a) print id(a[0])==id(b[0]) now i get true as output.Since the address of a[0] and b[0] is…
ashwin murthy
  • 59
  • 1
  • 1
  • 6
2
votes
2 answers

For loop in python doesn't manipulate original objects, instead performs deep copy

I'm doing a for loop over a list in Python, which to all of my knowledge should create a shallow copy of each element in the list. However, when I perform operations on elements, the changes aren't being reflected in the original list. Here's the…
bendl
  • 1,583
  • 1
  • 18
  • 41
2
votes
2 answers

Provide simplest example where deep copy is needed in ruby

I really don't understand the difference between shallow and deep copy. Ruby's #dup seems to create a deep copy when I test it. Documentation says: Produces a shallow copy of obj---the instance variables of obj are copied, but not the objects they…
Marko Avlijaš
  • 1,579
  • 13
  • 27