deep-copy

Python: deepcopy does not work on user-defined classes?

落花浮王杯 提交于 2019-11-29 13:32:46
In the following example I would expect deepcopy to create a copy of field and not just copy the reference. What happens here and is there an easy way around it? from copy import deepcopy class Test: field = [(1,2)] t1 = Test() t2 = deepcopy(t1) t2.field[0]=(5,10) print t1.field # [(1,2)] expected but [(5,10)] obtained print t2.field # [(5,10)] expected Output: [(5, 10)] [(5, 10)] Deep copying (by default) only applies to instance level attributes - not class level - It doesn't make much sense that there's more than one unique class.attribute ... Change your code to: class Test: def __init__

R, deep vs. shallow copies, pass by reference

本秂侑毒 提交于 2019-11-29 11:19:17
问题 I would like to understand the logic R uses when passing arguments to functions, creating copies of variables, etc. with respect to the memory usage. When does it actually create a copy of the variable vs. just passing a reference to that variable? In particular the situations I am curious about are: f <- function(x) {x+1} a <- 1 f(a) Is a being passed literally or is a reference to a being passed? x <- 1 y <- x Reference of copy? When is this not the case? If someone could explain this to me

python multiprocessing arguments: deep copy?

僤鯓⒐⒋嵵緔 提交于 2019-11-29 09:12:24
from multiprocessing import Process # c is a container p = Process(target = f, args = (c,)) p.start() I assume a deep copy of c is passed to function f because shallow copy would make no sense in the case of a new process (the new process doesn't have access to the data from the calling process). But how is this deep copy defined? There is a whole set of notes in the copy.deepcopy() documentation, do all these notes apply here as well? The multiprocessing documentation says nothing... When you create a Process instance, under the hood Python issues a fork() . This creates a child process whose

How to create a Bitmap deep copy

∥☆過路亽.° 提交于 2019-11-29 09:09:13
I'm dealing with Bitmaps in my application and for some purposes I need to create a deep copy of the Bitmap. Is there an elegant way how to do it? I tried Bitmap deepCopy = original.Clone(); ,well apparently this doesn't create a deep copy, but shallow one. My next attempt was to create a new Bitmap Bitmap deepCopy = new Bitmap(original); Unfortunately this constructor is Bitmap(Image), not Bitmap(Bitmap) and Bitmap(Image) will convert my nice 8bppIndexed Pixelformat into a different one. Another attempt was to use of a MemoryStream public static Bitmap CreateBitmapDeepCopy(Bitmap source) {

Creating a completely new copy of bitmap from a bitmap in C#

倖福魔咒の 提交于 2019-11-29 07:57:25
I need a deep copy of bitmap from another bitmap. Now, most of the solutions say something like this , which is not a deep copy . Meaning that when I lock the original bitmap, then the copy gets locked too, as the clone is a shallow copy of the original bitmap. Now the following seems to work for me, but I am not sure that will work in all cases. public static Bitmap GetCopyOf(Bitmap originalImage) { Rectangle rect = new Rectangle(0, 0, originalImage.Width, originalImage.Height); Bitmap retrunImage = new Bitmap(originalImage.Width, originalImage.Height, originalImage.PixelFormat); BitmapData

Java deep copy library

一个人想着一个人 提交于 2019-11-29 07:43:52
Is there library that can make deep copy? ex) normal object, array, list, inputstream etc. @Konrad's posting is spot on. The only general way of doing deep copying is to use a Java serialization mechanism. Obviously, it is expensive. The other caveat is that some Java objects are impossible to copy by serialization. Examples include Thread and subclasses cannot be serialized because a thread's execution state cannot be serialized. Streams in general cannot be serialized because you cannot get at the state of the stream that has already been written (writers, output streams) or that is yet to

Python dictionary deepcopy

孤人 提交于 2019-11-29 06:06:11
问题 I was wondering in how does exactly deepcopy work in the following context: from copy import deepcopy def copyExample: self.myDict = {} firstPosition = "First" firstPositionContent = ["first", "primero"] secondPosition = "Second" secondPositionContent = ["second"] self.myDict[firstPosition] = firstPositionContent self.myDict[secondPosition] = secondPositionContent return deepcopy(self.myDict) def addExample(self): copy = self.copyExample() copy["Second"].add("segundo") Does it return the

How to make a deep copy of an InputStream in Java [closed]

吃可爱长大的小学妹 提交于 2019-11-29 04:38:05
I would like to know how to make a deep copy of an InputStream . I know that it can be done with IOUtils packages, but I would like to avoid them if possible. Does anyone know an alternate way? InputStream is abstract and does not expose (neither do its children) internal data objects. So the only way to "deep copy" the InputStream is to create ByteArrayOutputStream and after doing read() on InputStream, write() this data to ByteArrayOutputStream. Then do: newStream = new ByteArrayInputStream(byteArrayOutputStream.toArray()); If you are using mark() on your InputStream then indeed you can not

Deep Copy of a C# Object

大憨熊 提交于 2019-11-29 04:26:38
I am working on some code that is written in C#. In this app, I have a custom collection defined as follows: public class ResultList<T> : IEnumerable<T> { public List<T> Results { get; set; } public decimal CenterLatitude { get; set; } public decimal CenterLongitude { get; set; } } The type used by Results are one of three custom types. The properties of each of the custom types are just primitive types (ints, strings, bools, int?, bool?). Here is an example of one of the custom types: public class ResultItem { public int ID { get; set; } public string Name { get; set; } public bool? isLegit {

Deep copying array of nested objects in javascript [duplicate]

孤街醉人 提交于 2019-11-28 21:22:23
This question already has an answer here: What is the most efficient way to deep clone an object in JavaScript? 69 answers I am trying to deep copy array of nested objects in javascript. My array look like this var arr = [{name:"adam",age:"21"}, {name:"freddie",age:"35",children:[{name:"mercury",age:"25"}]}, {name:"jim",age:"35",children:[{name:"morrison",age:"25",children:[{name:"some", age:"40"}]}]} ]; I want to make a deep copy of every object inside the array that is i want to create a exact copy of arr into new array which should not have object reference. Depth of array is also unknown