我正在尝试将一个类对象存储在字典中但由于某种原因我不能这样做。
?>
为了澄清,x是一个普通/简单类定义的字符串,"人类"是类的名称。运行上面的代码时出现以下错误。
local_env = {}
global_env = {}
exec(x, global_env, local_env)
dill.dumps(local_env['Human'])
答案 0 :(得分:1)
我是dill
作者。简短的回答是它不起作用,因为dill
不知道如何。
>>> import dill
>>> local_env = {}
>>> global_env = {}
>>> x = "class Human(object):\n pass"
>>> exec(x, global_env, local_env)
>>> local_env
{'Human': <class 'Human'>}
>>> dill.dumps(local_env['Human'])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/mmckerns/lib/python2.7/site-packages/dill-0.2.5.dev0-py2.7.egg/dill/dill.py", line 243, in dumps
dump(obj, file, protocol, byref, fmode, recurse)#, strictio)
File "/Users/mmckerns/lib/python2.7/site-packages/dill-0.2.5.dev0-py2.7.egg/dill/dill.py", line 236, in dump
pik.dump(obj)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 224, in dump
self.save(obj)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "/Users/mmckerns/lib/python2.7/site-packages/dill-0.2.5.dev0-py2.7.egg/dill/dill.py", line 1231, in save_type
StockPickler.save_global(pickler, obj)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 754, in save_global
(obj, module, name))
pickle.PicklingError: Can't pickle <class 'Human'>: it's not found as __builtin__.Human
答案越长,pickle
只能序列化在文件内定义但不在解释器中定义的类,或者更常见的是__main__
。但是,dill
通常可以腌制这些动态构建的类,因为它将__main__
视为文件,并解析python的内置缓冲区以获取类定义(以及其他内容)。将类定义隐藏在字符串中,然后使用exec
生成类......好吧,这是dill
无法处理的情况。由于同样的原因,dill
也无法挑选exec
内定义的函数。但是,这两种情况都是有效的。
已经有功能票证(https://github.com/uqfoundation/dill/issues/145)...所以我会在票证上添加这个问题的链接。
答案 1 :(得分:0)
import dill
import builtins
x = """
def test(x, y):
return x + y
"""
env = {}
env['__builtins__'] = builtins
local_env = Environment()
exec(x, env, local_env)
exec("__builtins__.{0} = {0}".format('test'), env, local_env)
test = dill.loads(dill.dumps(local_env['test']))
使用Mike给出的答案作为灵感,我想出了一个解决方法。