[Python-3000] Last call for PEP 3137: Immutable Bytes andMutable Buffer

Terry Reedy tjreedy at udel.edu
Tue Oct 2 17:59:07 CEST 2007


"Christian Heimes" <lists at cheimes.de> wrote in message 
news:fdstov$av5$1 at sea.gmane.org...
| Terry Reedy wrote:
| > If orig_data were mutable (the new buffer, as proposed in the PEP), 
would
| > not
| >
| > for i in range(len(orig_data)):
| >   orig_data[i] &= 0x1F
| >
| > do it in place? (I don't have .0a1 to try on the current bytes.)
|
| Good catch!
|
| Python 3.0a1 (py3k:58282, Sep 29 2007, 15:07:57)
| [GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2
| >>> orig_data = b"abc"
| >>> orig_data
| b'abc'
| >>> for i in range(len(orig_data)):
| ...   orig_data[i] &= 0x1F
| ...
| >>> orig_data
| b'\x01\x02\x03'

Thanks for testing this!  Glad it worked.  This sort of thing makes having 
bytes/buffer[i] an int a plus.  (Just noticed, PEP accepted.)

| It'd be useful and more efficient if the new buffer type would support
| the bit wise operations directly:
|
| >>> orig_data &= 0x1F
| TypeError: unsupported operand type(s) for &=: 'bytes' and 'int'

This sort of broadcast behavior seems like numpy territory to me.  Or 
better for a buffer subclass.  Write it first in Python, using loops like 
above (partly for documentation and other implementations), then in C when 
interest and usage warrents.

| >>> orig_data &= b"\x1F"
| TypeError: unsupported operand type(s) for &=: 'bytes' and 'bytes'

Ugh is my response.  Stick with the first ;-).

Terry Jan Reedy





More information about the Python-3000 mailing list