[Python-ideas] Adding `Unpicklable` to the `collections` module

Antoine Pitrou solipsis at pitrou.net
Tue Nov 23 21:54:58 CET 2010


On Tue, 23 Nov 2010 22:46:51 +0200
cool-RR <cool-rr at cool-rr.com> wrote:
> (Spelling note: People told me that "pickleable" (with an "e" in the middle)
> makes more sense, so I'm using that now.)
> 
> The best solution might be to have both a `Pickleable` class and
> an `Unpickleable` class. The reason to have the former is that
> `isinstance(thing, Pickleable)` is more natural, and the reason to have the
> latter is because we can't require people to inherit from `Pickleable` for
> every single class that they define. (Since pickleability is the rule and
> unpickleability is the exception.)
> 
> So `Pickleable` could have a `__subclasshook__` that would do the real work,
> similarly to `Iterable`.

The problem is that "doing the real work" can be CPU-intensive, and I
don't think we would like isinstance() calls to become arbitrarily
expensive (even if technically it's possible).

An alternative is simply to catch the exception when trying to pickle a
value. That implies that the type takes care to raise an exception if
pickling would work but unpickling wouldn't, which is what file objects
now do.

By the way, "Unpickleable" doesn't work since it's ambiguous: you don't
know whether it means you can unpickle the thing, or you can't pickle
it.

Regards

Antoine.





More information about the Python-ideas mailing list