为什么不能腌制发电机? [英] Why can't generators be pickled?
问题描述
Python的泡菜(我在这里说的是标准的Python 2.5/2.6/2.7)不能泡菜锁,文件对象等.
Python's pickle (I'm talking standard Python 2.5/2.6/2.7 here) cannot pickle locks, file objects etc.
它也不能腌制生成器和lambda表达式(或任何其他匿名代码),因为腌制实际上仅存储名称引用.
It also cannot pickle generators and lambda expressions (or any other anonymous code), because the pickle really only stores name references.
对于锁和与操作系统相关的功能,为什么您无法腌制它们的原因很明显并且很有意义.
In case of locks and OS-dependent features, the reason why you cannot pickle them is obvious and makes sense.
但是您为什么不能腌制发电机?
注意:为清楚起见-我对根本原因(或用于该设计决策的假设和选择)感兴趣,为什么,而不是因为它会给您泡菜错误".
Note: just for clarity -- I'm interested in the fundamental reason (or assumptions and choices that went into that design decision) why, not in "because it gives you a Pickle error".
我意识到这个问题有点宽泛,因此,以下经验法则是您是否回答的:如果提出这些假设,或者允许的发生器类型受到更多限制,酸洗发生器会再次起作用吗?"
I realize the question's a bit wide-aimed, so here's a rule of thumb of whether your answered it: "If these assumptions were raised, or the type of allowed generator somehow more restricted, would pickling generators work again?"
推荐答案
有很多与此有关的信息.有关该问题的正式用语",请阅读(已关闭)Python bugtracker问题.
There is lots of information about this available. For the "official word" on the issue, read the (closed) Python bugtracker issue.