且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

酸洗错误:不能酸洗<type 'function'>

更新时间:2022-06-02 09:19:55

这个错误意味着你试图腌制一个内置的 FunctionType……而不是函数本身.这可能是由于某个地方的编码错误导致了函数的类而不是函数本身.

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__
foo
p0
.'
>>> pickle.dumps(foo, -1)
'x80x02c__main__
foo
qx00.'

如果您有一个 FunctionType 对象,那么您需要做的就是获取该类的一个实例——即像 foo 这样的函数.

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.