为什么“或”和“|”运营商共享相同的“__or__”魔术方法?

时间:2014-11-03 17:51:27

标签: python python-3.x

我知道|or运算符在python中具有不同的含义和行为。 E.g:

>>> s1 = {1, 2, 3}
>>> s2 = {10, 20, 30}
>>> s1 or s2
{1, 2, 3}
>>> s1 | s2
{1, 2, 3, 20, 10, 30}

但两家运营商共享相同的__or____ror__魔术方法。为什么呢?

1 个答案:

答案 0 :(得分:5)

由于or短路,因此该运算符有 no 魔术挂钩方法。这同样适用于and

换句话说,__or____ror____ior__ 仅用于按位| 。同样,__and____rand____iand__仅用于按位&