Proper application of the buffer interface
Mark Hammond
MHammond at skippinet.com.au
Thu Aug 12 09:56:10 EDT 1999
About a week ago, I wrote in message
<006501bedeee$33ef5c20$1101a8c0 at bobcat>...
>Just to extend my guesswork somewhat, there is a new built-in "buffer()"
>function. This returns a "buffer" object. I speculate this should be used
>in preference to Python strings when you have binary data. As this buffer
>object supports the buffer interfaces, they are basically as functional as
>strings for this purpose, but clearly indicate the data is not really a
>string! This appears to be more a matter of style, and also paves the road
>to Unicode - eg, it makes sense to convert any Python string object to
>Unicode, but not necessarily a binary buffer.
Just to prove it was guess-work, Bill Tutt indirectly pointed me at my error
here.
I may have given the impression that the buffer object returns a completely
new object - similar to a string. The reason I may have given that
impression is because it is exactly what I meant. However, it is wrong (and
I did know it was wrong <sigh>)
The "buffer()" built-in returns a read-only buffer for an _existing_ object.
It would only be an effective replacement for a string as a "byte buffer"
when coupled with an array object.
>>> import array
>>> a=array.array("b", range(ord('A'), ord('G')))
>>> b=buffer(a)
>>> b
<read-only buffer for fdcca0, ptr fdcc90, size 6 at fdccd0>
>>> b[:-1]
'ABCDE'
>>>
Just thought it worth a clarification.
As a side note, this also makes it far more difficult for extensions to
replace the use of strings with a different object for "byte buffers" - it
appears PyArray_New does not exist, so extension module writers are forced
to implement their own object if they wish to use the buffer interface, but
not use a PyString.
Mark.
More information about the Python-list
mailing list