how to make classes with __getattr__ pickable

馋奶兔 提交于 2019-12-05 17:20:46

The problem here is that your __getattr__ method is poorly implemented. It assumes that self.foo exists. If self.foo doesn't exist, trying to access it ends up calling __getattr__ - which results in infinite recursion:

>>> bar = Bar({})  # no `foo` attribute
>>> bar.x
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "untitled.py", line 19, in __getattr__
    return getattr(self.foo, attr)
  File "untitled.py", line 19, in __getattr__
    return getattr(self.foo, attr)
  File "untitled.py", line 19, in __getattr__
    return getattr(self.foo, attr)
  [Previous line repeated 329 more times]
RecursionError: maximum recursion depth exceeded while calling a Python object

To fix this, you have to throw an AttributeError if no foo attribute exists:

def __getattr__(self, attr):
    """If attr is not in channel, look in timing_data
    """
    if 'foo' not in vars(self):
        raise AttributeError
    return getattr(self.foo, attr)

(I used the vars function to get the object's dict, because it looks nicer than self.__dict__.)


Now everything works as expected:

dct={'a':1,'b':2,'c':3}
foo=Foo(dct)
dct2={'d':1,'e':2,'f':3,'foo':foo}
bar=Bar(dct2)
data = pickle.dumps(bar)
bar = pickle.loads(data)
print(vars(bar))
# output:
# {'d': 1, 'e': 2, 'f': 3, 'foo': <__main__.Foo object at 0x7f040fc7e7f0>}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!