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
32
votes
6 answers

What does this notation do for lists in Python: "someList[:]"?

I sometimes get across this way of printing or returning a list - someList[:]. I don't see why people use it, as it returns the full list. Why not simply write someList, whithout the [:] part?
Petr S
  • 303
  • 3
  • 7
30
votes
2 answers

C# Shallow copy Dictionary?

I need to shallow copy a dictionary in c#. For instance: Dictionary flags = new Dictionary(); flags[1] = 2; flags[2] = 3; flags[0] = 9001; Dictionary flagsn = flags.MemberwiseClone(); Unfortunately, that returns the…
Georges Oates Larsen
  • 6,812
  • 12
  • 51
  • 67
27
votes
2 answers

Shallow clone with JGIT

How I can do git clone --depth 1 ... with JGIT library?
Timothy Klim
  • 1,257
  • 15
  • 25
26
votes
7 answers

clearing or set null to objects in java

I was recently looking into freeing up memory occupied by Java objects. While doing that I got confused about how objects are copied (shallow/deep) in Java and how to avoid accidently clearing/nullifying objects while they are still in use. Consider…
as.tek
  • 937
  • 3
  • 14
  • 20
25
votes
4 answers

How do strings work when shallow copying something in C#?

Strings are considered reference types yet can act like values. When shallow copying something either manually or with the MemberwiseClone(), how are strings handled? Are they considred separate and isolated from the copy and master?
danmine
  • 11,325
  • 17
  • 55
  • 75
19
votes
3 answers

Shallow copy and deep copy in C

I tried googling this but only objected oriented languages pop up as results. From my understanding a shallow copy is copying certain members of a struct. so lets say a struct is typedef struct node { char **ok; int hi; int yep; …
ShadyBears
  • 3,955
  • 13
  • 44
  • 66
18
votes
3 answers

How to copy a list in Scala

I want to shallow copy a list in Scala. I wanted to do somehing like: val myList = List("foo", "bar") val myListCopy = myList.clone But the clone method is protected.
Maxime
  • 2,048
  • 1
  • 27
  • 38
17
votes
4 answers

Does Javascript slice method return a shallow copy?

In a Mozilla developer translated Korean lang says 'slice method' returns a new array copied shallowly. so I tested my code. var animals = ['ant', 'bison', 'camel', 'duck', 'elephant']; var t = animals.slice(2,4); console.log(t); t[0] =…
SeongUk Mun
  • 213
  • 3
  • 8
17
votes
6 answers

Python read-only lists using the property decorator

Short Version Can I make a read-only list using Python's property system? Long Version I have created a Python class that has a list as a member. Internally, I would like it to do something every time the list is modified. If this were C++, I would…
ngb
  • 183
  • 1
  • 1
  • 8
16
votes
8 answers

Is clone() in java shallow copy?

Is clone() in java a shallow copy? Eventually this gets to the clone() method of Object (the uppermost class), which creates a new instance of the same class as the object and copies all the fields to the new instance (a "shallow…
Josh Morrison
  • 7,488
  • 25
  • 67
  • 86
15
votes
4 answers

Is pass-by-value/reference equivalent to making a deep/shallow copy, respectively?

To reword the question in case someone types it into the search bar differently: Is pass-by-value the same as making a deep copy, and is pass-by-reference the same as making a shallow copy? If not, what is the difference? In Python, the language I'm…
15
votes
4 answers

Does a copy constructor/operator/function need to make clear which copy variant it implements?

Yesterday I asked a question about copying objects in C#, and most answers focussed on the difference between deep copy and shallow copy, and the fact that it should be made clear which of both copy variants a given copy constructor (or operator, or…
Dimitri C.
  • 21,861
  • 21
  • 85
  • 101
14
votes
1 answer

Why is copying a list using a slice[:] faster than using the obvious way?

Why is shallow-copying a list using a slice so much faster than using list builtin? In [1]: x = range(10) In [2]: timeit x_ = x[:] 10000000 loops, best of 3: 83.2 ns per loop In [3]: timeit x_ = list(x) 10000000 loops, best of 3: 147 ns per…
wim
  • 338,267
  • 99
  • 616
  • 750
13
votes
5 answers

Copy object properties: reflection or serialization - which is faster?

I have two objects of the same type and need to copy property values from one object to another. There are two options: Use reflection, navigate through the properties of the first object and copy the values. Serialize the first object and…
net_prog
  • 9,921
  • 16
  • 55
  • 70
13
votes
5 answers

Git cannot create shallow-since locally

I am trying to create a --shallow-since working clone from a local bare clone but it keeps pulling everything. --depth=N works fine. I'm thinking the issue is I'm using the wrong format? I've tried searching but no where does it explicitly say what…
arthur doe
  • 281
  • 1
  • 2
  • 6
1
2
3
26 27