MaxB MaxB - 2 months ago 5
Python Question

How come Python can pickle functions, but not modules?

I noticed that when my object contains an explicit reference to a module,

it will fail because of this.

However, if I stick a reference to a function from that module into my object instead, it can be picked and unpickled successfully.

How come Python can pickle functions, but not modules?


Because they didn't code support for it. C level types (and even modules written in Python are implemented with a C level type) require pickle support to be coded explicitly.

It's not very easy to determine what should be pickled if a module is allowed to be pickled; importing the same name on the other side would seem simple, but if you're actually trying to pickle the module itself, the worry would be that you want to pickle module state as well. It's even more confusing if the module is a C extension module, where module state may not even be exposed to Python itself, only used internally at the C layer.

Given that usually you want specific things from a module, not the whole module (which is usually not referenced as state, just imported at the top level), the benefits of supporting pickling for modules are limited, and the semantics are unclear, they haven't bothered to implement it.