如何复制具有特殊值的字典?

时间:2017-10-16 03:37:59

标签: python

数据:

mydict = {'__weakref__': <attribute '__weakref__' of 'Foo' objects>,\
 '__doc__': None, 'name': 'Guido'}

我正在尝试制作副本:

import copy
copy_dict = copy.deepcopy(mydict)

意外错误报告:

TypeError: can't pickle getset_descriptor objects

如果dict = {'name':'Guido'},则有效

>>> copy.deepcopy(dict)
{'name': 'Guido'}

问题是什么?

1 个答案:

答案 0 :(得分:0)

将词典的值保留在引号中。下面的代码在python2.7中找到

>>> mydict = {'__weakref__': "<attribute '__weakref__' of 'Foo' objects>",\
 '__doc__': None, 'name': 'Guido'}
>>> copy_dict = copy.deepcopy(mydict)
>>> copy_dict
{'__weakref__': "<attribute '__weakref__' of 'Foo' objects>", '__doc__': None, 'name': 'Guido'}
>>>