ispicklable() --> bool, possible?
Gerrit Holl
gerrit at nl.linux.org
Tue Feb 25 05:43:43 EST 2003
Jp Calderone schreef op dinsdag 25 februari om 01:57:30 +0000:
> I would use just this last portion
>
> > try:
> > cPickle.dumps(obj)
> > except (TypeError, cPickle.UnpickleableError):
> > return False
> > else:
> > return True
>
> Did you discard this approach for performance reasons?
No, I discard it for another reason. I am calling picklable()
from __getstate__, because if it isn't picklable, I need to do
some operation on it. One of the attributes of my instance is
another instance of the same superclass. So if I call picklable(),
the function tries to pickle the object. When trying to pickle it,
the __getstate__ method calls picklable() on its attributes, and
one of the attributes is, again, another instance of the same superclass.
This way, I get an infinite loop.
In reality, I am using the pygame library. Each sprite contains an
attribute listing the groups it's a member of, and each group contains
an attribute listing the sprites living in the group. Additionaly,
some of my sprites (bullets) contain an attribute 'origin' which is
a reference to another sprite instance. I could of course check in
my __getstate__ whether an attribute is called '_Sprite__g', 'origin'
or something else, but that approach is very ugly and importable (I
will need to change it every time I change my internal API).
yours,
Gerrit.
--
Asperger Syndroom - een persoonlijke benadering:
http://people.nl.linux.org/~gerrit/
Het zijn tijden om je zelf met politiek te bemoeien:
http://www.sp.nl/
More information about the Python-list
mailing list