酸洗过程是确定性的吗?
对于特定的输入值,Pickle 是否总是产生相同的输出? 我想当腌制具有相同内容但不同插入/删除历史记录的字典时可能会出现问题。 我的目标是使用 Pickle 和 SHA1 创建函数参数的“签名”,以实现 memoize。
Does Pickle always produce the same output for a certain input value? I suppose there could be a gotcha when pickling dictionaries that have the same contents but different insert/delete histories. My goal is to create a "signature" of function arguments, using Pickle and SHA1, for a memoize implementation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
右:
另请参阅:pickle.dumps 不适合散列
这存在许多基本问题。 不可能提出正确映射相等性的对象到字符串的转换 - 想想对象标识的问题:
根据您的具体要求,您可以将对象层次结构转换为可以进行散列的层次结构:
Right:
See also: pickle.dumps not suitable for hashing
There's a number of fundamental problems with this. It's impossible to come up with an object-to-string transformation that maps equality correctly—think of the problem of object identity:
Depending on your exact requirements, you may be able to transform object hierarchies into ones that you could then hash:
相同的输出是什么意思? 通常,您应该始终获得相同的往返输出(pickling -> unpickling),但我不认为序列化格式本身在每种情况下都保证相同。 当然,它可能会在平台之间发生变化等等。
在程序的一次运行中,使用 pickling 进行记忆应该没问题 - 我已经多次使用此方案,没有遇到任何问题,但那是针对非常简单的问题。 一个问题是,这并没有涵盖所有有用的情况(我想到的是函数:你无法对它们进行腌制,所以如果你的函数采用可调用参数,那将不起作用)。
What do you mean by same output ? You should normally always get the same output for a roundtrip (pickling -> unpickling), but I don't think the serialized format itself is guaranteed to be the same in every condition. Certainly, it may change between platforms and all that.
Within one run of your program, using pickling for memoization should be fine - I have used this scheme several times without trouble, but that was for quite simple problems. One problem is that this does not cover every useful case (function come to mind: you cannot pickle them, so if your function takes a callable argument, that won't work).