Reputation: 12665
I have a list of mutable objects which is an attribute of a class.
self.matriceCaracteristiques
I would like to keep a copy of it, so that the objects will change during execution as for the original list, but not their order in the list itself (that is what I want to preserve and "restore" after execution).
copy_of_matCar = self.matriceCaracteristiques[:] #to preserve the order of the objects
#that will be changed during execution
When it's time to restore the list, I've tried making this:
self.matriceCaracteristiques = copy_of_matCar[:]
but it doesn't work cause although the copy_of_matCar
has a different order (specifically, the one that the attribute had before some code execution), the other self.matriceCaracteristiques
remains exactly the same although the instruction.
So I have thought to make a deepcopy of it, by following the Python reference:
import copy
self.matriceCaracteristiques = copy.deepcopy(copy_of_matCar)
However, what I get is the following error:
self.matriceCaracteristiques = copy.deepcopy(copy_of_matCar)
AttributeError: 'function' object has no attribute 'deepcopy'
Any idea how I can fix this problem and get a deepcopy of the list copy_of_matCar
to be assigned to the self.matriceCaracteristiques
one?
Upvotes: 9
Views: 30708
Reputation: 1
from copy import deepcopy
#Using just copy the lists end up the same due to mutability
names = [["David"], ["Lais"], ["George"]]
names2 = names.copy()
names2[2][0] = "Fred"
print(names)
print(names2)
[['David'], ['Lais'], ['Fred']]
[['David'], ['Lais'], ['Fred']]
#Using deepcopy the lists stay unique
names = [["David"], ["Lais"], ["George"]]
names2 = deepcopy(names)
names2[2][0] = "Fred"
print(names)
print(names2)
[['David'], ['Lais'], ['George']]
[['David'], ['Lais'], ['Fred']]
Upvotes: -1
Reputation: 11
Please find other copy module in your code. For example, ...
from numpy.lib.function_base import copy
Please remove other copy modules.
Upvotes: 1
Reputation: 1294
What's been suggested in the comment is the source of the problem: there's something shadowing copy
in your module, after you imported the copy
module.
Consider for instance the following modules:
# In some_module_1.py
from copy import copy
# In some_module_2.py
import copy
# `copy` refers to the module, as expected
print('deepcopy' in dir(copy)) # True
from some_module_1 import *
# `copy` got shadowed, it is now actually referring to `copy.copy`
print('deepcopy' in dir(copy)) # False
Because I imported everything from some_module_1
, I also imported the function copy
I imported over there. This means it now shadows the module copy
imported 4 lines above. Therefore python properly complains that deepcopy
isn't an attribute of the function copy.copy
, hence the error
AttributeError: 'function' object has no attribute 'deepcopy'
Upvotes: 5
Reputation: 131
I am facing the same problem, and tried many ways. The following way resolves my problem: change
import copy
dict = {...}
copy.deepcopy()
to
from copy import deepcopy
dict = {...}
deepcopy()
Upvotes: 13