[Python-ideas] Bitwise operations on bytes class

Stefan Behnel stefan_ml at behnel.de
Mon Jun 16 21:25:58 CEST 2014


Nathaniel McCallum, 16.06.2014 20:03:
> I find myself, fairly often, needing to perform bitwise operations
> (rshift, lshift, and, or, xor) on arrays of bytes in python (both bytes
> and bytearray). I can't think of any other reasonable use for these
> operators. Is upstream Python interested in this kind of behavior by
> default? At the least, it would make many algorithms very easy to read
> and write.

ISTM that what you're asking for is essentially a SIMD data type, which
certainly has a lot of nice applications. However, restricting it to byte
values seems to be a rather niche use case to me. IMHO, this seems much
better suited for the array module than the "bytes as in string" general
purpose bytes type. The array module has support for all sorts of C-ish
integer types.

Different ways to handle errors (e.g. overflows) across the array would be
another reason to not push this into the bytes type.

Stefan




More information about the Python-ideas mailing list