shallow-copy

Creating numpy shallow copies with arithmetic operations

Creating numpy shallow copies with arithmetic operations Question: I noticed that array operations with an identity elements return a copy (possibly a shallow copy) of the array. Consider the code snippet below. a=np.arange(16).reshape([4,4]) print(a) b=a+0 print(b) a[2,2]=200 print(a) print(b) We see that b is a shallow copy of a. I don’t know if it is …

Total answers: 1

Shouldn't shallow copy create an object with different id in python?

Shouldn't shallow copy create an object with different id in python? Question: list1 = [1,2,3] list2 =list1 id(list1[0]) == id(list2[0]) #True id(list1) == id(list2) #True I am trying to learn the concept of deep copy and shallow copy. I understand that list1 and list2 would be references to the same memory, therefore id(list1[0]) will be …

Total answers: 1

Is it possible to make wrapper object for numbers, e.g. float, to make it mutable?

Is it possible to make wrapper object for numbers, e.g. float, to make it mutable? Question: In Python 3 everything is supposed to be an object, even numbers, but they’re immutable. Is it possible to create wrapper object for numbers, e.g. float, such that it would behave exactly as ordinary numbers except it must be …

Total answers: 2

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

What does this notation do for lists in Python: "someList[:]"? Question: 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? Asked By: Petr S || Source Answers: To …

Total answers: 6

Python read-only lists using the property decorator

Python read-only lists using the property decorator Question: 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 …

Total answers: 6

Python list slice syntax used for no obvious reason

Python list slice syntax used for no obvious reason Question: I occasionally see the list slice syntax used in Python code like this: newList = oldList[:] Surely this is just the same as: newList = oldList Or am I missing something? Asked By: Charles Anderson || Source Answers: [:] Shallow copies the list, making a …

Total answers: 5