python,list,slice,notation,shallow-copy
[:] creates a slice, usually used to get just a part of a list. Without any minimum/maximum index given, it creates a copy of the entire list. Here's a Python session demonstrating it: >>> a = [1,2,3] >>> b1 = a >>> b2 = a[:] >>> b1.append(50) >>> b2.append(51) >>>...
c#,clone,pass-by-reference,pass-by-value,shallow-copy
Why are objects automatically passed by reference? They're not. Is there any particular benefit from forcing the cloning process for them instead of treating objects more like int, double, boolean, etc. in these cases? There's no "cloning process" for reference types, only for value types. I think you're confusing...
python,networkx,deep-copy,shallow-copy
You can make a shallow copy by using the class constructor. E.g. for graphs, In [1]: import networkx as nx In [2]: G = nx.Graph() In [3]: G.add_edge(1,2,l=['a','b','c']) In [4]: H = nx.Graph(G) # shallow copy In [5]: H[1][2]['l'] Out[5]: ['a', 'b', 'c'] In [6]: H[1][2]['l'].append('d') In [7]: H[1][2]['l'] Out[7]:...
for entry in list_dict: dict1['x']['y'] = entry['a'] list1.append(dict1.copy()) >>>print list1 [{'x': {'y': 2}}, {'x': {'y': 2}}] Back to square one - it doesn't work! So, this is the first question - why did it stop working? Because, you now have three dictionaries: {'x': {'y': 2}} {'x': {'y': 2}} {'y': 2}...
You need to make field1 and field2 instance attributes of classA: def __init__(self): self.field1 = 0 self.field2 = 0 Without prefixing the names with self., field1 and field2 will be created as local names that are only available inside the __init__ method. Also, it looks like you should actually be...
python,properties,getter,shallow-copy
You could have method return a wrapper around your original list -- collections.Sequence might be of help for writing it. Or, you could return a tuple -- The overhead of copying a list into a tuple is often negligible. Ultimately though, if a user wants to change the underlying list,...
c++,copy-constructor,shallow-copy,default-copy-constructor
Since you want the default behavior for most members and only need special handling for one (static) member, why not encapsulate that special handling in its own class and make a member variable of that class? Like this: template<typename T> class InstanceCounter { public: static int Count; // Automatically invoked...
python,python-2.7,encapsulation,shallow-copy
The problem is that you're not copying the array before modifying it, you're just copying a reference to the array. After this line: self.linesfixed = templines Both self.linesfixed and templines refer to the same array. To get the intended behavior, you need to clone the array: self.linesfixed = list(templines) ...
c,malloc,free,dynamic-memory-allocation,shallow-copy
Accessing memory after you've free()d it invokes undefined behaviour. You should not be using the printf() with the free()d memory after you call free(pc1.p); ...
java,clone,copy-constructor,deep-copy,shallow-copy
That's how the copy constructor and the clone method should be: For the student: //Copy constructor for the student Student(Student copyCons){ this._rollNo = copyCons._rollNo; this._name = copyCons._name; this._address = copyCons._address; this._teacher = copyCons._teacher.clone(); } //Clone for the student protected Student clone(){ return new Student(this); } For the teacher: //This is...