Pickling error: Can't pickle

前端 未结 1 1878
心在旅途
心在旅途 2021-01-03 07:38

I am wondering what this error might mean:

PicklingError: Can\'t pickle : attribute lookup __builtin__.function failed
相关标签:
1条回答
  • 2021-01-03 08:14

    The error means you are trying to pickle a builtin FunctionType… not the function itself. It's likely do to a coding error somewhere picking up the class of the function instead of the function itself.

    >>> import sys
    >>> import pickle
    >>> import types
    >>> types.FunctionType
    <type 'function'>
    >>> try:
    ...     pickle.dumps(types.FunctionType)
    ... except:
    ...     print sys.exc_info()[1]
    ... 
    Can't pickle <type 'function'>: it's not found as __builtin__.function
    >>> def foo(x):
    ...   return x
    ... 
    >>> try:
    ...     pickle.dumps(type(foo))
    ... except:
    ...     print sys.exc_info()[1]
    ... 
    Can't pickle <type 'function'>: it's not found as __builtin__.function
    >>> try:
    ...     pickle.dumps(foo.__class__)
    ... except:
    ...     print sys.exc_info()[1]
    ... 
    Can't pickle <type 'function'>: it's not found as __builtin__.function
    >>> pickle.dumps(foo)
    'c__main__\nfoo\np0\n.'
    >>> pickle.dumps(foo, -1)
    '\x80\x02c__main__\nfoo\nq\x00.'
    

    If you have a FunctionType object, then all you need to do is get one of the instances of that class -- i.e. a function like foo.

    0 讨论(0)
提交回复
热议问题