Pickling a class with a __getattr__

  • Thread starter Peter Bengtsson
  • Start date
P

Peter Bengtsson

Hi, I'm trying to pickle an object instance of a class that is like a
dict but with a __getattr__ and I'm getting pickling errors.

This works but is not good enough.

$ python2.4.... pass
....{'age': 40, 'name': 'Zahid'}

This is what happens when I'm trying to be clever:
.... def __getattr__(self, key):
.... return self.__getitem__(key)
....Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/lib/python2.4/copy_reg.py", line 73, in _reduce_ex
getstate = self.__getstate__
File "<stdin>", line 3, in __getattr__
KeyError: '__getstate__'


Why can't I pickle the slightly more featurefull class there called
'Dict'? I've got my reasons for not going for a simple type dict but
feel that that is irrelevant right now.
 
P

Peter Otten

Peter said:
Hi, I'm trying to pickle an object instance of a class that is like a
dict but with a __getattr__ and I'm getting pickling errors.
This is what happens when I'm trying to be clever:

... def __getattr__(self, key):
... return self.__getitem__(key)
...
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/lib/python2.4/copy_reg.py", line 73, in _reduce_ex
getstate = self.__getstate__
File "<stdin>", line 3, in __getattr__
KeyError: '__getstate__'


Why can't I pickle the slightly more featurefull class there called
'Dict'?

Because you allow your __getattr__() implementation to raise the wrong kind
of exception.
.... def __getattr__(self, key):
.... try:
.... return self[key]
.... except KeyError:
.... raise AttributeError
....{'age': 42, 'name': 'Zaphod'}

Peter
 
P

Peter Bengtsson

Peter said:
Hi, I'm trying to pickle an object instance of a class that is like a
dict but with a __getattr__ and I'm getting pickling errors.
This is what happens when I'm trying to be clever:
... def __getattr__(self, key):
... return self.__getitem__(key)
...
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/lib/python2.4/copy_reg.py", line 73, in _reduce_ex
getstate = self.__getstate__
File "<stdin>", line 3, in __getattr__
KeyError: '__getstate__'
Why can't I pickle the slightly more featurefull class there called
'Dict'?

Because you allow your __getattr__() implementation to raise the wrong kind
of exception.

... def __getattr__(self, key):
... try:
... return self[key]
... except KeyError:
... raise AttributeError
...>>> friend = Dict(name="Zaphod", age=42)
{'age': 42, 'name': 'Zaphod'}

Peter

Thanks! That did the trick. I also noticed that I could define
__getstate__ and __setstate__ (needed for protocol 2) but your
solution works much better.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top