Python中的插入符(^)有什么作用?

问题:Python中的插入符(^)有什么作用?

我今天在python中遇到了插入符号运算符,并对其进行了尝试,得到了以下输出:

>>> 8^3
11
>>> 8^4
12
>>> 8^1
9
>>> 8^0
8
>>> 7^1
6
>>> 7^2
5
>>> 7^7
0
>>> 7^8
15
>>> 9^1
8
>>> 16^1
17
>>> 15^1
14
>>>

它似乎基于8,所以我猜某种字节操作?除了对浮点数的奇怪表现之外,我似乎无法找到更多关于此搜索网站的信息,是否有人链接到该运算符的工作,或者您可以在此处进行解释?

I ran across the caret operator in python today and trying it out, I got the following output:

>>> 8^3
11
>>> 8^4
12
>>> 8^1
9
>>> 8^0
8
>>> 7^1
6
>>> 7^2
5
>>> 7^7
0
>>> 7^8
15
>>> 9^1
8
>>> 16^1
17
>>> 15^1
14
>>>

It seems to be based on 8, so I’m guessing some sort of byte operation? I can’t seem to find much about this searching sites other than it behaves oddly for floats, does anybody have a link to what this operator does or can you explain it here?


回答 0

这是按位异或(异或)。

如果一个操作数(仅一个)(评估为)为true,则结果为true。

展示:

>>> 0^0
0
>>> 1^1
0
>>> 1^0
1
>>> 0^1
1

要解释您自己的示例之一:

>>> 8^3
11

这样考虑:

1000#8(二进制)
0011#3(二进制)
----#应用XOR(“垂直”)
1011#结果= 11(二进制)

It’s a bitwise XOR (exclusive OR).

It results to true if one (and only one) of the operands (evaluates to) true.

To demonstrate:

>>> 0^0
0
>>> 1^1
0
>>> 1^0
1
>>> 0^1
1

To explain one of your own examples:

>>> 8^3
11

Think about it this way:

1000  # 8 (binary)
0011  # 3 (binary)
----  # APPLY XOR ('vertically')
1011  # result = 11 (binary)

回答 1

它根据需要调用对象的__xor__()or __rxor__()方法,对于整数类型,它按位进行异或。

It invokes the __xor__() or __rxor__() method of the object as needed, which for integer types does a bitwise exclusive-or.


回答 2

这是一点一点的异或。《 Python语言参考》的第5章介绍了二进制按位运算符。

It’s a bit-by-bit exclusive-or. Binary bitwise operators are documented in chapter 5 of the Python Language Reference.


回答 3

一般来说,符号^是or 方法的中版本。无论在符号的左右放置什么数据类型,都必须以兼容的方式实现此功能。对于整数,这是常见的操作,但是例如没有一个内置的类型定义的函数类型:__xor____rxor__XORfloatint

In [12]: 3 ^ 4
Out[12]: 7

In [13]: 3.3 ^ 4
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-13-858cc886783d> in <module>()
----> 1 3.3 ^ 4

TypeError: unsupported operand type(s) for ^: 'float' and 'int'

关于Python的一件整洁的事情是,您可以在自己的类中重写此行为。例如,在某些语言中,^符号表示幂。您可以通过这种方式做到这一点,就像一个示例:

class Foo(float):
    def __xor__(self, other):
        return self ** other

然后,类似的事情将起作用,现在,对于Fooonly的实例,该^符号将表示幂。

In [16]: x = Foo(3)

In [17]: x
Out[17]: 3.0

In [18]: x ^ 4
Out[18]: 81.0

Generally speaking, the symbol ^ is an infix version of the __xor__ or __rxor__ methods. Whatever data types are placed to the right and left of the symbol must implement this function in a compatible way. For integers, it is the common XOR operation, but for example there is not a built-in definition of the function for type float with type int:

In [12]: 3 ^ 4
Out[12]: 7

In [13]: 3.3 ^ 4
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-13-858cc886783d> in <module>()
----> 1 3.3 ^ 4

TypeError: unsupported operand type(s) for ^: 'float' and 'int'

One neat thing about Python is that you can override this behavior in a class of your own. For example, in some languages the ^ symbol means exponentiation. You could do that this way, just as one example:

class Foo(float):
    def __xor__(self, other):
        return self ** other

Then something like this will work, and now, for instances of Foo only, the ^ symbol will mean exponentiation.

In [16]: x = Foo(3)

In [17]: x
Out[17]: 3.0

In [18]: x ^ 4
Out[18]: 81.0

回答 4

当您使用^操作员时,幕后方法__xor__便会调用。

a^b 相当于 a.__xor__(b)

此外,a ^= b等价于a = a.__ixor__(b)__xor____ixor__通过使用隐式调用时,其中用作后备^=但不存在)。

原则上,什么__xor__完全取决于其实施。Python中的常见用例是:

  • 集的对称差异(所有元素恰好存在于两个集之一)

演示:

>>> a = {1, 2, 3}
>>> b = {1, 4, 5}
>>> a^b
{2, 3, 4, 5}
>>> a.symmetric_difference(b)
{2, 3, 4, 5}
  • 两个整数的位的按位不等于

演示:

>>> a = 5
>>> b = 6
>>> a^b
3

说明:

    101 (5 decimal)
XOR 110 (6 decimal)
-------------------
    011 (3 decimal)

When you use the ^ operator, behind the curtains the method __xor__ is called.

a^b is equivalent to a.__xor__(b).

Also, a ^= b is equivalent to a = a.__ixor__(b) (where __xor__ is used as a fallback when __ixor__ is implicitly called via using ^= but does not exist).

In principle, what __xor__ does is completely up to its implementation. Common use cases in Python are:

  • Symmetric Difference of sets (all elements present in exactly one of two sets)

Demo:

>>> a = {1, 2, 3}
>>> b = {1, 4, 5}
>>> a^b
{2, 3, 4, 5}
>>> a.symmetric_difference(b)
{2, 3, 4, 5}
  • Bitwise Non-Equal for the bits of two integers

Demo:

>>> a = 5
>>> b = 6
>>> a^b
3

Explanation:

    101 (5 decimal)
XOR 110 (6 decimal)
-------------------
    011 (3 decimal)