[英]Is `pickle.dump(d, f)` equivalent to `f.write(pickle.dumps(d))`?
Given an arbitrary picklable Python data structure data
, is 给定一个任意可选择的Python数据结构data
,是
with open('a', 'bw') as f:
f.write(pickle.dumps(data))
equivalent to 相当于
with open('a', 'bw') as f:
pickle.dump(data, f)
ie can I assume this equivalence when writing code? 即在编写代码时我可以假设这种等价吗? Could you describe how you reached to this conclusion? 你能描述一下你是如何得出这个结论的吗?
The use-case is to separate serialization from writing, since I may want to write pickle to a non-disk location. 用例是将序列化与写入分开,因为我可能想将pickle写入非磁盘位置。
Yes, they are equivalent. 是的,它们是等价的。 Looking at the source code of pickle.py
: 看一下pickle.py
的源代码:
def _dump(obj, file, protocol=None, *, fix_imports=True):
_Pickler(file, protocol, fix_imports=fix_imports).dump(obj)
def _dumps(obj, protocol=None, *, fix_imports=True):
f = io.BytesIO()
_Pickler(f, protocol, fix_imports=fix_imports).dump(obj)
res = f.getvalue()
assert isinstance(res, bytes_types)
return res
dumps
does the exact same thing as dump
, just with an io.BytesIO
object instead of a file object. dumps
做同样的事情作为dump
,只是用io.BytesIO
对象,而不是一个文件对象。 It calls the internal _Pickler().dump()
in the same way and simply returns the contents of the io.BytesIO
object. 它以相同的方式调用内部_Pickler().dump()
并简单地返回io.BytesIO
对象的内容。 Therefore, all f.write(pickle.dumps(data))
does is first forward the result to an io.BytesIO
object then to the actual file instead of writing to the file directly. 因此,所有f.write(pickle.dumps(data))
都会先将结果转发给io.BytesIO
对象,然后转发到实际文件,而不是直接写入文件。
They are not equivalent if an exception occurs during pickling. 如果在酸洗过程中发生异常,它们不相等。 f.write(pickle.dumps(data))
will not write anything to the file. f.write(pickle.dumps(data))
不会向文件写入任何内容。 Whereas pickle.dump(data, f)
will end up with a snapped or partial pickle. 而pickle.dump(data, f)
将以pickle.dump(data, f)
或partial pickle结束。
Yes. 是。
The documentation of dumps is pretty clear: 转储文档非常清楚:
Return the pickled representation of the object as a bytes object, instead of writing it to a file. 将对象的pickled表示作为bytes对象返回,而不是将其写入文件。
Writing the same representation to a file will be the same as calling pickle.dump
directly. 将相同的表示写入文件将与直接调用pickle.dump
相同。
Also, "file" here means anything with a write
method, from documentation of dump : 另外,“file”在这里表示任何带有write
方法的东西,来自dump的文档 :
The file argument must have a write() method that accepts a single bytes argument. file参数必须具有接受单个字节参数的write()方法。 It can thus be an on-disk file opened for binary writing, an io.BytesIO instance, or any other custom object that meets this interface. 因此,它可以是为二进制写入打开的磁盘文件,io.BytesIO实例或满足此接口的任何其他自定义对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.