我们从Python开源项目中,提取了以下6个代码示例,用于说明如何使用copy_reg.__newobj__()。
def test_issue24097(self): # Slot name is freed inside __getattr__ and is later used. class S(str): # Not interned pass class A(object): __slotnames__ = [S('spam')] def __getattr__(self, attr): if attr == 'spam': A.__slotnames__[:] = [S('spam')] return 42 else: raise AttributeError import copy_reg expected = (copy_reg.__newobj__, (A,), ({}, {'spam': 42}), None, None) self.assertEqual(A().__reduce__(2), expected)
def __reduce_ex__(self, proto): state = (self.requires_grad, self.volatile, self._backward_hooks) if proto > 1: return type(self), (self.data,), state if sys.version_info[0] == 2: from copy_reg import __newobj__ else: from copyreg import __newobj__ return __newobj__, (type(self), self.data), state
def __reduce_ex__(self, proto): state = (self.requires_grad, False, self._backward_hooks) if proto > 1: return type(self), (self.data,), state if sys.version_info[0] == 2: from copy_reg import __newobj__ else: from copyreg import __newobj__ return __newobj__, (type(self), self.data), state