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 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 buffer has been replaced by the better named memoryview in Python 3, though you can use either in 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.

Answer from Scott Griffiths on Stack Overflow
๐ŸŒ
Python Reference
python-reference.readthedocs.io โ€บ en โ€บ latest โ€บ docs โ€บ functions โ€บ buffer.html
buffer โ€” Python Reference (The Right Way) 0.1 documentation
The buffer() function allows direct (read-only) access to an objectโ€™s byte-oriented data without needing to copy it first. That can yield large performance gains when operating on large objects since it does not create a copy of an object when slicing.
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ c-api โ€บ buffer.html
Buffer Protocol โ€” Python 3.14.8 documentation
Handle buffer requests for an exporter that wants to expose buf of size len with writability set according to readonly. buf is interpreted as a sequence of unsigned bytes. The flags argument indicates the request type.
Discussions

What is the Python 'buffer' type for? - Stack Overflow
But I still don't quite understand ... between buffering and simple slicing. Using s[6:11] doesn't take extra storage space either, am I wrong? 2010-08-06T11:31:57.627Z+00:00 ... In general a slice will take extra storage, so yes s[6:11] will be a copy. If you set t = s[6:11] and then del s, it frees the memory that was taken by s, proving that t was copied. (To see this you need a bigger s and track Python's memory ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
What is Buffer?
It is really context dependent word. In post-USSR countries it even means tits in slang :) But regarding programming - buffer usually means some memory, where you store values to access them next. Classical use-case - buffered I/O operations. Write to disk is slow in comparing to write to memory, if program will be writing to disk byte by byte it will be running too long, so it write to special buffer in memory, from where it will be pick-up by another thread or OS routine and written to disk. It is transparent for user: he press save button, and program continue to be responsible, he can continue to do whatever he need. But if program will be interrupted before buffers really be save to HDD/SSD - file could be corrupted. More on reddit.com
๐ŸŒ r/learnpython
5
9
June 16, 2021
Buffer function for python 3+ - Stack Overflow
I'm trying to open a vtk window using vtk_show, but my Ipython console crashes every time i do this, apparently this is because Ipython can't display an external window, which is exactly what vtk_s... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Some data getting lost when using PySerial's read() function
in_waiting gives the number of bytes in the input buffer. The reason the number starts high is probably that the MCU probably starts sending before you start reading, so it's starting to fill up? This could also be related to why you sometimes get partial packets, you might be filling up the buffer, and it's not a multiple of 76 bytes, so the last message added to the buffer is truncated. More on reddit.com
๐ŸŒ r/learnpython
5
2
April 19, 2024
๐ŸŒ
Python Data Science Handbook
jakevdp.github.io โ€บ blog โ€บ 2014 โ€บ 05 โ€บ 05 โ€บ introduction-to-the-python-buffer-protocol
An Introduction to the Python Buffer Protocol | Pythonic Perambulations
The item formats are extremely flexible: you can basically use any of the codes within Python's struct module, which gives you nearly endless possibilities for passing around structured data. For more complicated arrays with multiple dimensions, you may have to allocate arrays for the shape, strides, etc. In that case, it's possible to define a release_buffer function which deallocates these arrays when the buffer is no longer needed.
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ python โ€บ buffer interface in python
Buffer in Python | Delft Stack
March 11, 2025 - This interface defines a set of methods that allow you to access the raw bytes of an object, making it easier to work with binary data. By utilizing the buffer interface, you can read and write data more efficiently, leveraging Pythonโ€™s capabilities to handle low-level operations seamlessly.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ what is buffer?
r/learnpython on Reddit: What is Buffer?
June 16, 2021 -

How can you explain to the beginner what is buffer? I have been searching about the buffer but its so deep for me to analyze their wordings.

Top answer
1 of 5
11
It is really context dependent word. In post-USSR countries it even means tits in slang :) But regarding programming - buffer usually means some memory, where you store values to access them next. Classical use-case - buffered I/O operations. Write to disk is slow in comparing to write to memory, if program will be writing to disk byte by byte it will be running too long, so it write to special buffer in memory, from where it will be pick-up by another thread or OS routine and written to disk. It is transparent for user: he press save button, and program continue to be responsible, he can continue to do whatever he need. But if program will be interrupted before buffers really be save to HDD/SSD - file could be corrupted.
2 of 5
4
You: hey I need some memory for a value I'm trying to store, can I get idk 20 bytes? Operating system: you can have 20 bytes, here's where they are insert address here You: great! Here's 20 characters worth of text OS: thanks I'll hold onto that for you. You: actually I need a few more bytes... Got any space? OS: well sure but I'll have to make a new address and get rid of the first one You: how about I just put 30 bytes in the original address? OS: nah fam it'll eat into someone else's space. You: well alright I'll just copy my data OS: cool, here's your new address Long story short, it's a section in your computer's memory set aside for some kind of data. The type of data isn't important, just has to be enough space. Python handles most of this already
Top answer
1 of 2
13

At least these two points must be modified on your code to have the same behavior with Python 3:

  • The buffer(...) built-in function in Python 2 has been replaced by memoryview(...) in Python 3: What is Python buffer type for?. Replace the buffer call by memoryview
  • the str(...) built-in function has to replaced by a bytes(...) call to get a bytes object: https://docs.python.org/2/howto/pyporting.html#text-versus-binary-data

So the data = ... line should read:

data = bytes(memoryview(writer.GetResult()))
2 of 2
1

To clarify, I believe this example was an adaptation of a very informative blog example showing how to extract surfaces from medical images using VTK's marching cubes algorithm. The accompanying Jupyter notebook was intended for Python 2.7, and as mentioned for it to be used in Python 3.6+, the data=... portion needs to be changed.

import vtk
from IPython.display import Image

def vtk_show(renderer, width=400, height=300):
    """
    Takes vtkRenderer instance and returns an IPython Image with the 
    rendering.
    """
    renderWindow = vtk.vtkRenderWindow()
    renderWindow.SetOffScreenRendering(1)
    renderWindow.AddRenderer(renderer)
    renderWindow.SetSize(width, height)
    renderWindow.Render()

    windowToImageFilter = vtk.vtkWindowToImageFilter()
    windowToImageFilter.SetInput(renderWindow)
    windowToImageFilter.Update()

    writer = vtk.vtkPNGWriter()
    writer.SetWriteToMemory(1)
    writer.SetInputConnection(windowToImageFilter.GetOutputPort())
    writer.Write()
    data = memoryview(writer.GetResults()).tobytes()

    return Image(data)

Credit for the solution definitely goes to @MafiaSkafia and @jcgiret, but I wanted to post a full and final solution.

Find elsewhere
๐ŸŒ
Medium
medium.com โ€บ @bramblexu โ€บ understand-the-buffer-policy-in-python-78e91e7759ca
Understand the Buffer Policy in Python | by Xu LIANG | Medium
May 31, 2020 - In [1]: f = open('line_buffer.txt', 'w', buffering=1) # text mode, line buffering โ€‹ In [2]: cat line_buffer.txt โ€‹ In [3]: f.write('this is line\n') # \n means we finish a like, so this line is wirtten to hard disk Out[3]: 13 โ€‹ In [4]: cat line_buffer.txt this is line โ€‹ In [5]: f.write('this not a line') # no \n, so it won't be wirtten to hard disk Out[5]: 15 โ€‹ In [6]: cat line_buffer.txt this is line โ€‹ In [7]: f.write('\n') # with \n, write to hard disk Out[7]: 1 โ€‹ In [8]: cat line_buffer.txt this is line this not a line
๐ŸŒ
Python
docs.python.org โ€บ 3.10 โ€บ c-api โ€บ buffer.html
Buffer Protocol โ€” Python 3.10.20 documentation
Handle buffer requests for an exporter that wants to expose buf of size len with writability set according to readonly. buf is interpreted as a sequence of unsigned bytes. The flags argument indicates the request type. This function always fills in view as specified by flags, unless buf has ...
๐ŸŒ
Python
docs.python.org โ€บ 3.7 โ€บ c-api โ€บ buffer.html
Buffer Protocol โ€” Python 3.7.17 documentation
Handle buffer requests for an exporter that wants to expose buf of size len with writability set according to readonly. buf is interpreted as a sequence of unsigned bytes. The flags argument indicates the request type. This function always fills in view as specified by flags, unless buf has ...
๐ŸŒ
Python
docs.python.org โ€บ 3.14 โ€บ c-api โ€บ buffer.html
Buffer Protocol โ€” Python 3.14.7 documentation
Handle buffer requests for an exporter that wants to expose buf of size len with writability set according to readonly. buf is interpreted as a sequence of unsigned bytes. The flags argument indicates the request type.
๐ŸŒ
Python
docs.python.org โ€บ 3.11 โ€บ c-api โ€บ buffer.html
Buffer Protocol โ€” Python 3.11.15 documentation
Handle buffer requests for an exporter that wants to expose buf of size len with writability set according to readonly. buf is interpreted as a sequence of unsigned bytes. The flags argument indicates the request type.
๐ŸŒ
Python
docs.python.org โ€บ 3.6 โ€บ c-api โ€บ buffer.html
Buffer Protocol โ€” Python 3.6.15 documentation
Return 1 if the memory defined by the view is C-style (order is 'C') or Fortran-style (order is 'F') contiguous or either one (order is 'A'). Return 0 otherwise. This function always succeeds. int PyBuffer_ToContiguous(void *buf, Py_buffer *src, Py_ssize_t len, char order)ยถ
๐ŸŒ
Python
docs.python.org โ€บ dev โ€บ c-api โ€บ buffer.html
Buffer Protocol โ€” Python 3.16.0a0 documentation
Handle buffer requests for an exporter that wants to expose buf of size len with writability set according to readonly. buf is interpreted as a sequence of unsigned bytes. The flags argument indicates the request type.
๐ŸŒ
Python
peps.python.org โ€บ pep-0688
PEP 688 โ€“ Making the buffer protocol accessible in Python | peps.python.org
April 23, 2022 - The CPython C API provides a versatile mechanism for accessing the underlying memory of an objectโ€”the buffer protocol introduced in PEP 3118. Functions that accept binary data are usually written to handle any object implementing the buffer protocol.
๐ŸŒ
Python
docs.python.org โ€บ 3.8 โ€บ c-api โ€บ buffer.html
Buffer Protocol โ€” Python 3.8.20 documentation
Handle buffer requests for an exporter that wants to expose buf of size len with writability set according to readonly. buf is interpreted as a sequence of unsigned bytes. The flags argument indicates the request type. This function always fills in view as specified by flags, unless buf has ...
๐ŸŒ
Real Python
realpython.com โ€บ ref โ€บ glossary โ€บ buffer-protocol
buffer protocol | Python Glossary โ€“ Real Python
A mechanism in Python that allows objects to share their internal memory buffers with other objects for direct access.
๐ŸŒ
Python
docs.python.org โ€บ 3.12 โ€บ c-api โ€บ objbuffer.html
Old Buffer Protocol โ€” Python 3.12.13 documentation
Returns a pointer to a read-only memory location usable as character-based input. The obj argument must support the single-segment character buffer interface. On success, returns 0, sets buffer to the memory location and buffer_len to the buffer ...