jepler.lnk at lnk.ispi.net
Sun Apr 23 04:31:20 CEST 2000
On 21 Apr 2000 14:06:28 -0500, Glyph Lefkowitz
<glyph at twistedmatrix.com> wrote:
>Perhaps I should ask this question in a different way. How does
>Pickle tell if an object is pickleable or not? Is there some
>pickle.ispicklable method I could refer to?
If you have control over the object hierarchy, why not use this
'unpicklable mixin' and convenience functions I just tossed together:
import pickle, UserList
"""This tests if a can be pickled (by actually trying to do so)"""
"""Note that this function does not tell whether a ultimately
contains an unpicklable instance. But at least it's a simple test.""
return isinstance(a, Unpicklable)
"""Note that this function does not tell whether an instance
might ultimately be unpicklable due to something nested inside it."""
return issubclass(a, Unpicklable)
class UserListU(UserList.UserList, Unpicklable): pass
x = UserList.UserList([1,2,3])
y = UserListU([1,2,3])
print pickle.dumps(y) # This will cause an exception
More information about the Python-list