Pickle hiccups

Wednesday 23 July 2003

(This is not about deli after-effects: it’s about Python). The pickle module is great: any Python data, simply serialized for storage, transmission, whatever. Nevertheless, Jarno writes about his difficulty with pickle. I don’t have the same problem (I always go find my last use of it to copy from anyway), but I had two deeper problems with pickle recently:

Problem one: You can’t pickle instances of classes that are not declared at the top level of their module. This is clearly stated in the docs, but I had to discover it the hard way. I like to use private classes where the class is only needed within another class, and pickle can’t handle it:

# Bad: can't pickle Outer
class Outer:
    class Inner:
        # ....

    def __init__(self):
        self.inner = self.Inner()
# Good: can pickle Outer
class _Inner:
    # ....

class Outer:
    def __init__(self):
        self.inner = _Inner()

Problem two: I had created an overly-tricky class that never returned an AttributeError: missing attributes simply returned None. This messed with pickle’s head, because it tries to get the __getstate__ method on instances, and None is not callable. This wasn’t really pickle’s fault, more of an object lesson in how to override the deep magic methods like __getattr__.

Comments

Add a comment:

Ignore this:
Leave this empty:
Name is required. Either email or web are required. Email won't be displayed and I won't spam you. Your web site won't be indexed by search engines.
Don't put anything here:
Leave this empty:
URLs auto-link and some tags are allowed: <a><b><i><p><br><pre>.