When can a Python object be pickled

前端 未结 4 1033
野性不改
野性不改 2021-02-19 08:20

I\'m doing a fair amount of parallel processing in Python using the multiprocessing module. I know certain objects CAN be pickle (thus passed as arguments in multi-p) and other

4条回答
  •  轮回少年
    2021-02-19 08:36

    From the docs:

    The following types can be pickled:

    • None, True, and False
    • integers, long integers, floating point numbers, complex numbers
    • normal and Unicode strings
    • tuples, lists, sets, and dictionaries containing only picklable objects
    • functions defined at the top level of a module
      • built-in functions defined at the top level of a module
    • classes that are defined at the top level of a module
    • instances of such classes whose __dict__ or the result of calling __getstate__() is picklable (see section The pickle protocol for details).

    Attempts to pickle unpicklable objects will raise the PicklingError exception; when this happens, an unspecified number of bytes may have already been written to the underlying file. Trying to pickle a highly recursive data structure may exceed the maximum recursion depth, a RuntimeError will be raised in this case. You can carefully raise this limit with sys.setrecursionlimit().

提交回复
热议问题