I wanted to define a class that returns None for unknown attributes with __getattr__ method.
After doing that, I am trying to dump an object of that class to a Pickle.
However, I got the error
Traceback (most recent call last):
File "c:\SVN\Scripts\Rally\examples\t_pickle_None.py", line 14, in <module>
pickle.dump(toto, f, pickle.HIGHEST_PROTOCOL)
TypeError: 'NoneType' object is not callable
Without defining __getattr__, it works fine, but I would like to keep this function.
Here is my code: how to make it work with __getattr__?
Thanks
import pickle
from typing import Any
class Toto:
def __init__(self, name:str) -> None:
self.name = name
def __getattr__(self, _: str) -> Any:
"""Return None for all unknown attributes"""
return None
toto = Toto("Toto")
with open('toto.pkl', 'wb') as f:
pickle.dump(toto, f, pickle.HIGHEST_PROTOCOL)
__getattr__. I wrote something about it here. Does it help?UserDict. I tried to make it work for my other problem ofmypycomplaining about dynamically created outputs butmypycomplains also about your code :) ... but this is a topic for another day (or more specifically another question).