【发布时间】:2019-03-15 18:32:45
【问题描述】:
基本上,我正在做的是
class A(object):
a = 1
b = 2
copy.deepcopy(dict(A.__dict__))
它给出了错误
TypeError: object.new(getset_descriptor) 不安全,请使用 getset_descriptor。new()
如果我更新如下代码并从类字典项中弹出 __dict__ 和 __weakref__
class A(object):
a = 1
b = 2
attrs = dict(A.__dict__)
attrs.pop("__weakref__")
attrs.pop("__dict__")
copy.deepcopy(dict(A.__dict__))
# It gives {'__module__': '__main__', 'a': 1, 'b': 2, '__doc__': None}
它工作正常,我知道这与复制 weakref 有关,这将是错误的。
有没有更好的方法来做同样的事情,我不需要专门弹出 __dict__ 和 __weakref__ 并且我只能得到 attributes 类,不包括魔法方法/属性?
【问题讨论】:
-
那些是属性。无论如何,您为什么要尝试执行深层复制?
-
什么是类的“属性”很滑。
-
@wim 属性是我在上面的例子中定义的属性,“a”和“b”是属性或者说类成员。
-
@user2357112 这是完成一些其他工作所需的要求。
标签: python python-2.7 class