[Python-ideas] Bitwise operations on bytes class
Nathaniel McCallum
npmccallum at redhat.com
Wed Jun 18 18:20:37 CEST 2014
On Wed, 2014-06-18 at 12:05 -0400, Alexander Belopolsky wrote:
>
> On Wed, Jun 18, 2014 at 11:51 AM, Antoine Pitrou <antoine at python.org>
> wrote:
> Rather than adding new operations to bytes/bytearray, an
> alternative is a separate type ("bitview"?) which would take a
> writable buffer as argument and then provide the operations
> over that buffer.
>
> +1
>
>
> .. and it does not have to be part of stdlib. The advantage of
> implementing this outside of stdlib is that users of older versions of
> Python will benefit immediately.
Older versions of Python can just do:
third = [a ^ b for a, b in zip(first, second)]
The problem is that this is more expensive and less readable than:
third = first ^ second
... or ...
first ^= second
I'm not making this proposal on the basis that something can't be done
already, but based on the fact that implementing it natively as part of
the base types is a natural growth of the language.
Of course this can be implemented in a module at the cost of "batteries
included," a new dependency, readability and perhaps some additional
overhead. I, for one, would not use such a module and would just
implement the operations myself (as I have done for the last several
years).
The reason for this proposal is that such operations seem to me to be
extremely natural to bytes/bytearray. And I think at least some others
agree.
Nathaniel
More information about the Python-ideas
mailing list