5

In the python documentation for struct, the word buffer is used without explanation:

http://docs.python.org/library/struct.html

struct.unpack_from(fmt, buffer[,offset=0])

Unpack the buffer according to the given format. The result is a tuple even if it contains exactly one item. The buffer must contain at least the amount of data required by the format (len(buffer[offset:]) must be at least calcsize(fmt)).

What is meant here with a buffer. Is a string a buffer, or a file descriptor? What methods must a 'buffer' have?

1 Answer 1

4

It's a memory buffer: in Python 2, a string (str), in Python 3, a binary string (bytes), or alternatively an object constructed with buffer.

Sign up to request clarification or add additional context in comments.

4 Comments

The buffer function talks about an object that supports the buffer call interface. What is this interface? Does a file descriptor match that, or should I always read into a string first?
python.org/dev/peps/pep-3118 A file descriptor or file-like object won't work.
You can use mmap to have a memory map to a file without having to read it into a file. docs.python.org/library/mmap.html
mmap won't work with remote filesystems, though, so be prepared to write a backup routine that does read in the file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.