import random
class Foo(object):
def __init__(self):
self._name = ''.join([chr(65 + random.randrange(0, 26)) for _ in range(3)])
self._data = None
def __getattr__(self, item):
dashitem = '_' + item
# if dhasattr(self, dashitem):
# is a bad idea because hasattr calls getattr
# is in self.__dict__.keys() also a bad idea?
if dashitem in self.__dict__.keys():
return self.__dict__[dashitem]
obj = Foo()
obj._data = [random.randrange(10, 100) for _ in range(random.randrange(1, 11))]
So far, so good. I can call obj.name and get back
obj._name`
In [2]: obj.name
Out[2]: 'QZB'
In [3]: obj.data
Out[3]: [54]
Then I try pickling the object:
import pickle
pickle.dumps(obj)
is a no-go though.
File "<ipython-input-7-a7748eba906b>", line 2, in <module>
pickle.dumps(obj)
File "/usr/local/Cellar/python/2.7.14_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 1380, in dumps
Pickler(file, protocol).dump(obj)
File "/usr/local/Cellar/python/2.7.14_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 224, in dump
self.save(obj)
File "/usr/local/Cellar/python/2.7.14_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 306, in save
rv = reduce(self.proto)
File "/Users/vishal/virtenvs/fm/bin/../lib/python2.7/copy_reg.py", line 84, in _reduce_ex
dict = getstate()
How do I do what I want with __getattr__
above (return _<attr>
if <attr>
is not found without breaking other normal behaviour??