copy.deepcopy raises TypeError on objects with self-defined __new__() method

后端 未结 3 1659
盖世英雄少女心
盖世英雄少女心 2021-01-17 20:42

I want to implement a symbol type, which keeps track of the symbols we already have(saved in _sym_table), and return them if they exist, or create new ones othe

3条回答
  •  逝去的感伤
    2021-01-17 21:04

    Seems to me you want the Symbol instances to be singletons. Deepcopy, however is supposed to be used when you want an exact copy of an instance, i.e. a different instance that is equal to the original.

    So the usage here kinda contradicts the purpose of deepcopy. If you want to make it work anyhow, you can define the __deepcopy__ method on Symbol.

提交回复
热议问题