I can "pickle local objects" if I use a derived class?

fonini

The pickle reference states that the set of objects which can be pickled is rather limited. Indeed, I have a function which returns a dinamically-generated class, and I found I can't pickle instances of that class:

>>> import pickle
>>> def f():
...     class A: pass
...     return A
... 
>>> LocalA = f()
>>> la = LocalA()
>>> with open('testing.pickle', 'wb') as f:
...     pickle.dump(la, f, pickle.HIGHEST_PROTOCOL)
... 
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
AttributeError: Can't pickle local object 'f.<locals>.A'

Such objects are too complicated for pickle. Ok. Now, what's magic is that, if I try to pickle a similar object, but of a derived class, it works!

>>> class DerivedA(LocalA): pass
... 
>>> da = DerivedA()
>>> with open('testing.pickle', 'wb') as f:
...     pickle.dump(da, f, pickle.HIGHEST_PROTOCOL)
...
>>>

What's happening here? If this is so easy, why doesn't pickle use this workaround to implement a dump method that allows "local objects" to be pickled?

Akshat Mahajan

I think you did not read the reference you cite carefully. The reference also clearly states that only the following objects are pickleable:

  • functions defined at the top level of a module (using def, not >lambda)
  • built-in functions defined at the top level of a module
  • classes that are defined at the top level of a module

Your example

>>> def f():
...     class A: pass
...     return A

does not define a class at the top level of a module, it defines a class within the scope of f(). pickle works on global classes, not local classes. This automatically fails the pickleable test.

DerivedA is a global class, so all is well.

As for why only top-level (global to you) classes and functions can't be pickled, the reference answers that question as well (bold mine):

Note that functions (built-in and user-defined) are pickled by “fully qualified” name reference, not by value. This means that only the function name is pickled, along with the name of the module the function is defined in. Neither the function’s code, nor any of its function attributes are pickled. Thus the defining module must be importable in the unpickling environment, and the module must contain the named object, otherwise an exception will be raised.

Similarly, classes are pickled by named reference, so the same restrictions in the unpickling environment apply.

So there you have it. pickle only serialises objects by name reference, not by the raw instructions contained within the object. This is because pickle's job is to serialise object hierarchy, and nothing else.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why can't I use alias from a base class in a derived class with templates?

From Dev

How can I friend a derived class function in the base class?

From Dev

How can I implement a Singleton class that can be derived from in WPF?

From Dev

How can I implement a Singleton class that can be derived from in WPF?

From Dev

Can I use CRTP with multiple derived classes, and use them polymorphically?

From Dev

How can I combine templated derived class in CRTP with derived class expression templates?

From Dev

How can I find out which method of a derived class is not implemented?

From Dev

how can I obtain HWND of a class derived from QMainWindow

From Dev

How can I do constructor overloading in a derived class in TypeScript?

From Dev

Why i can't access protected property in the derived class

From Dev

How can I retrieve the derived class from a list of abstract classes?

From Dev

Can I call a derived method from base class?

From Dev

How can I "un-JsonIgnore" an attribute in a derived class?

From Dev

how can I obtain HWND of a class derived from QMainWindow

From Dev

Can I forward template arguments of a derived class to the base in CRTP?

From Dev

How can I find out which method of a derived class is not implemented?

From Dev

How can I retrieve the derived class from a list of abstract classes?

From Dev

C++ How can I return an unknown derived class?

From Dev

How can I access a method defined with new keyword in a derived class

From Dev

How can I call the constructor of the derived class when using CRTP?

From Dev

Can I use __autoload in class?

From Dev

Can I use select from derived tables with JPA?

From Dev

Can I alias a member of the base class in derived class without increasing class memory?

From Dev

How do I override `toString` in my derived class and use the private instance variables from the base class?

From Dev

Can't use struct in derived templated class?

From Dev

Why can I call base template class method from derived class

From Dev

Can I cast a derived class to a private base class, using C-style cast?

From Dev

How can I store in a derived class information obtained during initialization of a base class?

From Dev

How can I create an instance of a derived class from an instance of a base class and include private fields?

Related Related

  1. 1

    Why can't I use alias from a base class in a derived class with templates?

  2. 2

    How can I friend a derived class function in the base class?

  3. 3

    How can I implement a Singleton class that can be derived from in WPF?

  4. 4

    How can I implement a Singleton class that can be derived from in WPF?

  5. 5

    Can I use CRTP with multiple derived classes, and use them polymorphically?

  6. 6

    How can I combine templated derived class in CRTP with derived class expression templates?

  7. 7

    How can I find out which method of a derived class is not implemented?

  8. 8

    how can I obtain HWND of a class derived from QMainWindow

  9. 9

    How can I do constructor overloading in a derived class in TypeScript?

  10. 10

    Why i can't access protected property in the derived class

  11. 11

    How can I retrieve the derived class from a list of abstract classes?

  12. 12

    Can I call a derived method from base class?

  13. 13

    How can I "un-JsonIgnore" an attribute in a derived class?

  14. 14

    how can I obtain HWND of a class derived from QMainWindow

  15. 15

    Can I forward template arguments of a derived class to the base in CRTP?

  16. 16

    How can I find out which method of a derived class is not implemented?

  17. 17

    How can I retrieve the derived class from a list of abstract classes?

  18. 18

    C++ How can I return an unknown derived class?

  19. 19

    How can I access a method defined with new keyword in a derived class

  20. 20

    How can I call the constructor of the derived class when using CRTP?

  21. 21

    Can I use __autoload in class?

  22. 22

    Can I use select from derived tables with JPA?

  23. 23

    Can I alias a member of the base class in derived class without increasing class memory?

  24. 24

    How do I override `toString` in my derived class and use the private instance variables from the base class?

  25. 25

    Can't use struct in derived templated class?

  26. 26

    Why can I call base template class method from derived class

  27. 27

    Can I cast a derived class to a private base class, using C-style cast?

  28. 28

    How can I store in a derived class information obtained during initialization of a base class?

  29. 29

    How can I create an instance of a derived class from an instance of a base class and include private fields?

HotTag

Archive