Python: the difference between "|" and "or" in the equation

all. I am running a simple python program, and I found that when I use followees = self.followee.get(userId, set()) | set([userId])it passed the test. However, if I use followees = self.followee.get(userId, set()) or set([userId]), it is not.

Thus, there are apparently some differences between the two operators in the equations. Does anyone know what is going on?

Thank!

+4
source share
1 answer

For sets (which are your operands here) |returns the union of both sets (operands), and the operator orreturns the first true operand (non-empty set) or the last if all operands are false, making it ora short-circuit operator.

Consider the following examples:

>>> set([1,2,3]) | set([4])
set([1, 2, 3, 4])
>>> set([1,2,3]) or set([4])
set([1, 2, 3])
>>> set([1,2,3]) or set([])
set([1, 2, 3])
>>> set([1,2,3]) | set([])
set([1, 2, 3])

, , , | or .

or , | - , __or__ __ror__ , [max-] collections.Counter .

+5

Source: https://habr.com/ru/post/1681045/


All Articles