为什么Python3.2不能深度复制(globals())?

时间:2013-02-07 12:04:42

标签: python deep-copy

我在python 3.2.2 IDLE中运行了这些代码:

from copy import deepcopy
deepcopy(globals())

我收到错误消息:

Traceback (most recent call last):
  File "H:\      \python\copy deepcopy.py", line 27, in <module>
    deepcopy(globals())
  File "D:\Program Files\Python32\lib\copy.py", line 147, in deepcopy
    y = copier(x, memo)
  File "D:\Program Files\Python32\lib\copy.py", line 236, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "D:\Program Files\Python32\lib\copy.py", line 174, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "D:\Program Files\Python32\lib\copy.py", line 290, in _reconstruct
    state = deepcopy(state, memo)
  File "D:\Program Files\Python32\lib\copy.py", line 147, in deepcopy
    y = copier(x, memo)
  File "D:\Program Files\Python32\lib\copy.py", line 236, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "D:\Program Files\Python32\lib\copy.py", line 174, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "D:\Program Files\Python32\lib\copy.py", line 285, in _reconstruct
    y = callable(*args)
  File "D:\Program Files\Python32\lib\copyreg.py", line 88, in __newobj__
    return cls.__new__(cls, *args)
TypeError: object.__new__(NotImplementedType) is not safe, use NotImplementedType.__new__()

所以我不知道为什么我们不能对globals()进行deepcopy()?

因为deepcopy()在执行时改变了全局变量dict?

1 个答案:

答案 0 :(得分:3)

不,这不是因为deepcopy在执行时更改了全局变量dict,但正如异常所示,在它的过程中,它会遇到特殊的NotImplemented built-in constant,键入NotImplementedType,它无法实例化并因此复制。