I was trying something with types.MappingProxyType
:
class MyClass:
l = [1, 2]
proxy = MyClass.__dict__
Then I wanted to extract dictionary from this Proxy:
d = proxy.copy()
print(type(d))
#<class 'dict'>
Then I wanted to append something to list value of d
:
d["l"].append(3)
But this also affects proxy
(for sure):
>>> print(proxy["l"])
[1, 2, 3]
I wanted to not affect our proxy
, so I tried using copy.deepcopy
:
import copy
dict_2 = copy.deepcopy(d)
And it gived an error:
Traceback (most recent call last):
File "C:\Users\Dinçel\AppData\Local\Programs\Python\Python37\istihza.py", line 18, in <module>
dict_2 = copy.deepcopy(d)
File "C:\Users\Dinçel\AppData\Local\Programs\Python\Python37\lib\copy.py", line 150, in deepcopy
y = copier(x, memo)
File "C:\Users\Dinçel\AppData\Local\Programs\Python\Python37\lib\copy.py", line 240, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "C:\Users\Dinçel\AppData\Local\Programs\Python\Python37\lib\copy.py", line 169, in deepcopy
rv = reductor(4)
TypeError: can't pickle getset_descriptor objects
I know that the copy module is trying to use functions which are used by pickle module for copying. https://docs.python.org/3/library/copy.html#copy.deepcopy
Classes can use the same interfaces to control copying that they use to control pickling. See the description of module pickle for information on these methods. In fact, the copy module uses the registered pickle functions from the copyreg module.
The problem is that, type of d
is dict
. And we can deepcopy dicts:
copy.deepcopy({"this":["is", "a", "dict"]})
So if type of d
is dict
, why I cant deepcopy it? Or is the type of d
is not really dict and is it only a hack in the Python?