Here's a line from a hash function I wrote:
M = tuple(buffer(M, i, Nb) for i in range(0, len(M), Nb))
This will split a long string, M, into shorter 'strings' of length Nb, where Nb is the number of bytes / characters I can handle at a time. It does this WITHOUT copying any parts of the string, as would happen if I made slices of the string like so:
M = tuple(M[i*Nb:i*Nb+Nb] for i in range(0, len(M), Nb))
I can now iterate over M just as I would had I sliced it:
H = key
for Mi in M:
H = encrypt(H, Mi)
Basically, buffers and memoryviews are efficient ways to deal with the immutability of strings in Python, and the general copying behavior of slicing etc. A memoryview is just like a buffer, except you can also write to it, not just read.
While the main buffer / memoryview doc is about the C implementation, the standard types page has a bit of info under memoryview: http://docs.python.org/library/stdtypes.html#memoryview-type
Edit: Found this in my bookmarks, http://webcache.googleusercontent.com/search?q=cache:Ago7BXl1_qUJ:mattgattis.com/2010/3/9/python-memory-views+site:mattgattis.com+python&hl=en&client=firefox-a&gl=us&strip=1 is a REALLY good brief writeup.
Edit 2: Turns out I got that link from When should a memoryview be used? in the first place, that question was never answered in detail and the link was dead, so hopefully this helps.
memoryview
type, or is there something I should absolutely know about it? And also: why is it not possible to write objects exposing the buffer interface? It's a design choice from Guido & Co. or it's an implicit limitation of python internal working? – Hintze