Python 缓冲区类型有什么用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3422685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:59:44  来源:igfitidea点击:

What is Python buffer type for?

pythonpython-2.7

提问by satoru

There is a buffertype in python, but I don't know how can I use it.

bufferpython中有一个类型,但我不知道如何使用它。

In the Python docthe description is:

Python 文档中,描述是:

buffer(object[, offset[, size]])

The object argument must be an object that supports the buffer call interface (such as strings, arrays, and buffers). A new buffer object will be created which references the object argument. The buffer object will be a slice from the beginning of object (or from the specified offset). The slice will extend to the end of object (or will have a length given by the size argument).

buffer(object[, offset[, size]])

object 参数必须是支持缓冲区调用接口的对象(如字符串、数组和缓冲区)。将创建一个引用 object 参数的新缓冲区对象。缓冲区对象将是从对象开头(或从指定的偏移量)开始的切片。切片将延伸到对象的末尾(或将具有由 size 参数给出的长度)。

采纳答案by Scott Griffiths

An example usage:

示例用法:

>>> s = 'Hello world'
>>> t = buffer(s, 6, 5)
>>> t
<read-only buffer for 0x10064a4b0, size 5, offset 6 at 0x100634ab0>
>>> print t
world

The buffer in this case is a sub-string, starting at position 6 with length 5, and it doesn't take extra storage space - it references a slice of the string.

在这种情况下,缓冲区是一个子字符串,从位置 6 开始,长度为 5,它不占用额外的存储空间 - 它引用字符串的一个切片。

This isn't very useful for short strings like this, but it can be necessary when using large amounts of data. This example uses a mutable bytearray:

这对于像这样的短字符串不是很有用,但在使用大量数据时可能是必要的。这个例子使用了一个可变的bytearray

>>> s = bytearray(1000000)   # a million zeroed bytes
>>> t = buffer(s, 1)         # slice cuts off the first byte
>>> s[1] = 5                 # set the second element in s
>>> t[0]                     # which is now also the first element in t!
'\x05'

This can be very helpful if you want to have more than one view on the data and don't want to (or can't) hold multiple copies in memory.

如果您想要对数据有多个视图并且不想(或不能)在内存中保存多个副本,这将非常有用。

Note that bufferhas been replaced by the better named memoryviewin Python 3, though you can use either in Python 2.7.

请注意,它buffer已被memoryviewPython 3 中更好的命名所取代,但您可以在 Python 2.7 中使用其中任何一个。

Note also that you can't implement a buffer interface for your own objects without delving into the C API, i.e. you can't do it in pure Python.

另请注意,如果不深入研究 C API,您就无法为自己的对象实现缓冲区接口,即您无法在纯 Python 中实现。

回答by Andre Holzner

I think buffers are e.g. useful when interfacing python to native libraries. (Guido van Rossum explains bufferin this mailinglist post).

我认为缓冲区在将 python 连接到本机库时很有用。(Guido van Rossumbuffer此邮件列表帖子中进行了解释)。

For example, numpy seems to use buffer for efficient data storage:

例如,numpy 似乎使用缓冲区来进行高效的数据存储:

import numpy
a = numpy.ndarray(1000000)

the a.datais a:

a.data是一个:

<read-write buffer for 0x1d7b410, size 8000000, offset 0 at 0x1e353b0>