Question

I am quite confused with the term 'buffer Interface' in python. what does it mean to say that " A python object exposing its underlying memory structure' can someone explain with an example. Thanks in advance

Was it helpful?

Solution

"Underlying memory structure" refers to the sequence of octets that comprise the object in computer memory. For example, when you create the string "abc", Python must reserve at least 3 bytes of memory and store the letters a, b, and c. If the memory is contiguous (as is the case for strings), its address and size can be passed to any piece of C code that wants to inspect it without going through the overhead of the Python str type.

A useful example is the array type. An array is a sequence that works much like a Python list, with the difference that it contains single-typed elements - you can have an array of ints and an array of floats, but you can't mix the two. The benefit is that arrays pack data as efficiently as possible, storing them in a flat C array. This array is exposed through the buffer interface - it allows one to query the exact memory position and size of the underlying C array, and pass it to some C function that will efficiently initialize it or write it out to disk. The numpy numeric package is built around this kind of data sharing between Python and C (and even FORTRAN), for which they extended the buffer protocol, and some of these extensions made it into Python 3.

The mmap object, which provides a Python interface to OS-level memory mapping functionality, also provides a buffer interface. This enables C code that must efficiently access the memory, such as the re module, to also work with memory-mapped regions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top