deep-copy

copy.deepcopy raises TypeError on objects with self-defined __new__() method

梦想的初衷 提交于 2019-12-01 15:12:33
问题 I want to implement a symbol type, which keeps track of the symbols we already have(saved in _sym_table ), and return them if they exist, or create new ones otherwise. The code: # -*- coding: utf-8 -*- _sym_table = {} class Symbol(object): def __new__(cls, sym): if sym not in _sym_table: return super().__new__(cls) else: return _sym_table[sym] def __init__(self, sym): self.sym = sym _sym_table[sym] = self def __str__(self): return self.sym def __cmp__(self, other): return self is other def _

Deep copying maps in Golang

随声附和 提交于 2019-12-01 13:56:12
From what I understand, maps are reference types in Go. So assignment will do shallow copy. I plan to do a recursive deep copy of Maps in golang. Recursive because I am dealing with a map that holds the unmarshalled contents of a JSON. func deepCopyJSON(src map[string]interface{}, dest *map[string]interface{}) error { if src == nil || dest == nil { return errors.New("src/dest is nil. You cannot insert to a nil map") } for key, value := range src { if reflect.TypeOf(value).String() != jsonType { (*dest)[key] = value } else { (*dest)[key] = make(map[string]int) //Suspect code below causes the

replacing items in nested lists python

╄→гoц情女王★ 提交于 2019-12-01 13:28:21
I am trying to make a duplicate list of lists and change one element to another within the nested lists of the duplicate list but am having some trouble. How I made the duplicate list: order = [['yhjK', 'F'], 'gap', ['bcsA', 'F'], ['bcsB', 'F'], ['bcsZ', 'F'], 'gap', ['yhjK', 'R']] #order_1 = list(order) #this makes the duplicate list as well order_1 = [] for x in order: order_1.append(x) How I changed the elements: for item in order_1: for n,i in enumerate(item): if i=='R': item[n]='F' if i=='F': item[n]='R' I want to replace all the 'F' with 'R' and vice versa. This accomplishes that but the

Deep copying maps in Golang

余生颓废 提交于 2019-12-01 12:48:11
问题 From what I understand, maps are reference types in Go. So assignment will do shallow copy. I plan to do a recursive deep copy of Maps in golang. Recursive because I am dealing with a map that holds the unmarshalled contents of a JSON. func deepCopyJSON(src map[string]interface{}, dest *map[string]interface{}) error { if src == nil || dest == nil { return errors.New("src/dest is nil. You cannot insert to a nil map") } for key, value := range src { if reflect.TypeOf(value).String() != jsonType

python copy.deepcopy lists seems shallow

杀马特。学长 韩版系。学妹 提交于 2019-12-01 12:07:21
I am trying to initialize a list of lists representing a 3x3 array: import copy m = copy.deepcopy(3*[3*[0]]) print(m) m[1][2] = 100 print(m) and the output is: [[0, 0, 0], [0, 0, 0], [0, 0, 0]] [[0, 0, 100], [0, 0, 100], [0, 0, 100]] which is not what I expected since the last elements of each row are shared! I did get the result I need by using: m = [ copy.deepcopy(3*[0]) for i in range(3) ] but I don't understand why the first (and simpler) form does not work. Isn't deepcopy supposed to be deep? The problem is that deepcopy keeps a memo that contains all instances that have been copied

python copy.deepcopy lists seems shallow

不羁的心 提交于 2019-12-01 10:11:33
问题 I am trying to initialize a list of lists representing a 3x3 array: import copy m = copy.deepcopy(3*[3*[0]]) print(m) m[1][2] = 100 print(m) and the output is: [[0, 0, 0], [0, 0, 0], [0, 0, 0]] [[0, 0, 100], [0, 0, 100], [0, 0, 100]] which is not what I expected since the last elements of each row are shared! I did get the result I need by using: m = [ copy.deepcopy(3*[0]) for i in range(3) ] but I don't understand why the first (and simpler) form does not work. Isn't deepcopy supposed to be

Side effects when passing objects to function in C++

戏子无情 提交于 2019-12-01 06:02:55
I have read in C++ : The Complete Reference book the following Even though objects are passed to functions by means of the normal call-by-value parameter passing mechanism, which, in theory, protects and insulates the calling argument, it is still possible for a side effect to occur that may affect, or even damage, the object used as an argument. For example, if an object used as an argument allocates memory and frees that memory when it is destroyed, then its local copy inside the function will free the same memory when its destructor is called. This will leave the original object damaged and

How to make deep copy of java object without using serialization ?

橙三吉。 提交于 2019-12-01 04:13:16
问题 Is it possible to make a deep copy/clone of a Java object without using serialization ? If so then how ? 回答1: You could use the Java Deep-Cloning Library to make deep copies of objects. It is really useful when you can't (or don't want) to make your classes serializable. The use is straight-forward: Cloner cloner = new Cloner(); MyClass clone = cloner.deepClone(o); // clone is a deep-clone of o 来源: https://stackoverflow.com/questions/25472281/how-to-make-deep-copy-of-java-object-without-using

Side effects when passing objects to function in C++

馋奶兔 提交于 2019-12-01 03:58:53
问题 I have read in C++ : The Complete Reference book the following Even though objects are passed to functions by means of the normal call-by-value parameter passing mechanism, which, in theory, protects and insulates the calling argument, it is still possible for a side effect to occur that may affect, or even damage, the object used as an argument. For example, if an object used as an argument allocates memory and frees that memory when it is destroyed, then its local copy inside the function

Relationship between pickle and deepcopy

给你一囗甜甜゛ 提交于 2019-12-01 02:15:52
What exactly is the relationship between pickle and copy.deepcopy ? What mechanisms do they share, and how? It is clear the two are closely-related operations, and share some of the mechanisms/protocols, but I can't wrap my head around the details. Some (confusing) things I found out: If a class defines __[gs]etstate__ , they get called upon a deepcopy of its instances. This surprised me at first, because I thought they are specific to pickle , but then I found that Classes can use the same interfaces to control copying that they use to control pickling . However, there's no documentation of