简体   繁体   中英

Why can't you xor bytes objects in python?

I think I understand python bytes objects, but supporting bitwise operations on byte strings seems like such an obvious feature. I don't understand why it is not supported.

>>>'abcdefg'.encode('ascii')
b'abcdefg'

Okay. I went from a string to something like the byte representation of my string in ascii.

So when I try:

>>> a = 'abcdefg'.encode('ascii')
>>> a ^ a
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for ^: 'bytes' and 'bytes'

Why? Why doesn't python support this? Is there something I don't understand about bytes objects that makes this unfeasible or ambiguous?

This feature has been proposed on the python bug tracker . And there is proposed patch (which I wrote after being annoyed by this). But the current feedback is negative, and more is needed if it is to be included.

它不受支持主要是因为它在“普通”Python代码中很少需要,并且当你需要它时,你可能已经在使用第三方软件包, 比如NumPy ,它已经构建了这样的东西(甚至更多!) -in和非常高效...至少比标准Python更高效。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM