ispicklable() --> bool, possible?

Jp Calderone exarkun at intarweb.us
Mon Feb 24 19:53:07 EST 2003


On Tue, Feb 25, 2003 at 12:10:36AM +0100, Gerrit Holl wrote:
> Hi,
> 
> I am currently using the following code to check whether an object is
> picklable or not:
> 
> _canpickle = (NoneType, bool, int, long, float, complex, basestring, FunctionType, ClassType)
> _cantpickle = (GeneratorType, pygame.sprite.Group, pygame.sprite.GroupSingle, pygame.sprite.Sprite, pygame.rect.Rect, pygame.Surface)
> def picklable(obj):
>     for inst in _canpickle:
>         if isinstance(obj, inst):
>             return True
>     for inst in _cantpickle:
>         if isinstance(obj, inst):
>             return False
>     if isinstance(obj, dict):
>         obj = obj.values()
>     if isinstance(obj, list) or isinstance(obj, tuple):
>         for subobj in obj:
>             if not picklable(subobj):
>                 return False
>         return True
>     debug("unknown", obj)

  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?

  Jp

-- 
 up 16 days, 4:29, 5 users, load average: 0.30, 0.12, 0.04
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20030224/23b0473a/attachment.sig>


More information about the Python-list mailing list