- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试 dill 一个存储在字典中的类对象,但由于某种原因我无法做到这一点。
local_env = {}
global_env = {}
exec(x, global_env, local_env)
dill.dumps(local_env['Human'])
为了澄清,x 是一个字符串,它是一个普通/简单的类定义,“Human”是类的名称。当我运行上面的代码时,出现以下错误。
------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/pickle.py in _getattribute(obj, name)
267 parent = obj
--> 268 obj = getattr(obj, subpath)
269 except AttributeError:
AttributeError: module 'builtins' has no attribute 'Human'
During handling of the above exception, another exception occurred:
AttributeError Traceback (most recent call last)
/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/pickle.py in save_global(self, obj, name)
906 module = sys.modules[module_name]
--> 907 obj2, parent = _getattribute(module, name)
908 except (ImportError, KeyError, AttributeError):
/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/pickle.py in _getattribute(obj, name)
270 raise AttributeError("Can't get attribute {!r} on {!r}"
--> 271 .format(name, obj))
272 return obj, parent
AttributeError: Can't get attribute 'Human' on <module 'builtins' (built-in)>
During handling of the above exception, another exception occurred:
PicklingError Traceback (most recent call last)
<ipython-input-27-6ce5592364a6> in <module>()
----> 1 dill.dumps(local_env['Human'])
/usr/local/lib/python3.5/site-packages/dill/dill.py in dumps(obj, protocol, byref, fmode, recurse)
241 """pickle an object to a string"""
242 file = StringIO()
--> 243 dump(obj, file, protocol, byref, fmode, recurse)#, strictio)
244 return file.getvalue()
245
/usr/local/lib/python3.5/site-packages/dill/dill.py in dump(obj, file, protocol, byref, fmode, recurse)
234 return
235 # end hack
--> 236 pik.dump(obj)
237 stack.clear() # clear record of 'recursion-sensitive' pickled objects
238 return
/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/pickle.py in dump(self, obj)
406 if self.proto >= 4:
407 self.framer.start_framing()
--> 408 self.save(obj)
409 self.write(STOP)
410 self.framer.end_framing()
/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/pickle.py in save(self, obj, save_persistent_id)
473 f = self.dispatch.get(t)
474 if f is not None:
--> 475 f(self, obj) # Call unbound method with explicit self
476 return
477
/usr/local/lib/python3.5/site-packages/dill/dill.py in save_type(pickler, obj)
1229 #print ("%s\n%s" % (type(obj), obj.__name__))
1230 #print ("%s\n%s" % (obj.__bases__, obj.__dict__))
-> 1231 StockPickler.save_global(pickler, obj)
1232 log.info("# T4")
1233 return
/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/pickle.py in save_global(self, obj, name)
909 raise PicklingError(
910 "Can't pickle %r: it's not found as %s.%s" %
--> 911 (obj, module_name, name))
912 else:
913 if obj2 is not obj:
PicklingError: Can't pickle <class 'Human'>: it's not found as builtins.Human
最佳答案
我是 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 通常可以 pickle 这类动态构建的类,因为它将 __main__ 视为文件,并解析 python 的内置缓冲区以获取类定义(以及其他)事物)。将类定义隐藏在字符串中,然后使用 exec 生成类……好吧,这是 dill
尚无法处理的情况。出于同样的原因,dill 也无法 pickle 在 exec 内定义的函数。然而,这两种情况都是有效的。
已经有一个函数票证 ( https://github.com/uqfoundation/dill/issues/145 )...所以我将在票证上添加指向此问题的链接。
关于python - 钻孔错误。在内建中找不到它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35320721/
例如,我有一个父类Author: class Author { String name static hasMany = [ fiction: Book,
代码如下: dojo.query(subNav.navClass).forEach(function(node, index, arr){ if(dojo.style(node, 'd
我有一个带有 Id 和姓名的学生表和一个带有 Id 和 friend Id 的 Friends 表。我想加入这两个表并找到学生的 friend 。 例如,Ashley 的 friend 是 Saman
我通过互联网浏览,但仍未找到问题的答案。应该很容易: class Parent { String name Child child } 当我有一个 child 对象时,如何获得它的 paren
我正在尝试创建一个以 Firebase 作为我的后端的社交应用。现在我正面临如何(在哪里?)找到 friend 功能的问题。 我有每个用户的邮件地址。 我可以访问用户的电话也预订。 在传统的后端中,我
我主要想澄清以下几点: 1。有人告诉我,在 iOS 5 及以下版本中,如果您使用 Game Center 设置多人游戏,则“查找 Facebook 好友”(如与好友争夺战)的功能不是内置的,因此您需要
关于redis docker镜像ENTRYPOINT脚本 docker-entrypoint.sh : #!/bin/sh set -e # first arg is `-f` or `--some-
我是一名优秀的程序员,十分优秀!