There is a
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).
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.
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
>>> s = bytearray(1000000) # a million zeroed bytes >>> t = buffer(s, 1) # slice cuts off the first byte >>> t = 5 # set the second element in t >>> s # which is now also the first element in s! '\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 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.