126

I see a "pipe" character (|) used in a function call:

res = c1.create(go, come, swim, "", startTime, endTime, "OK", ax|bx)

What is the meaning of the pipe in ax|bx?

Johannes Weiss
  • 52,533
  • 16
  • 102
  • 136
alwbtc
  • 28,057
  • 62
  • 134
  • 188

6 Answers6

200

This is also the union set operator

set([1,2]) | set([2,3])

This will result in set([1, 2, 3])

Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
gui11aume
  • 2,768
  • 3
  • 18
  • 23
91

It is a bitwise OR of integers. For example, if one or both of ax or bx are 1, this evaluates to 1, otherwise to 0. It also works on other integers, for example 15 | 128 = 143, i.e. 00001111 | 10000000 = 10001111 in binary.

Marc Mutz - mmutz
  • 24,485
  • 12
  • 80
  • 90
ahmet alp balkan
  • 42,679
  • 38
  • 138
  • 214
  • How about when I type 5|7 in the shell? It returns 7. why? – alwbtc May 13 '11 at 08:10
  • Because that's what bitwise OR does. – Ignacio Vazquez-Abrams May 13 '11 at 08:11
  • 2
    `5 = 101`, `7 = 111`, `101 | 111 = 111 = 7` – Paul R May 13 '11 at 08:13
  • 21
    @Ignacio: Python does not have a logical or operator?! What do you call [`or`](http://docs.python.org/reference/expressions.html#boolean-operations), then? – jscs May 13 '11 at 08:27
  • 2
    @Ignacio are you referring to it as a null coalescing operator (like in C#/Perl)? Never heard of it referred to that way, but if I understand correctly, that does make sense in a way since it's a short-circuit operator. Is that what you were referring to (do I understand you properly)? – Zach Kelling May 13 '11 at 09:28
  • 25
    @zeekay: Correct. Rather than always returning `True` or `False`, `and` and `or` always return one of their operands, hence "coalescing" rather than "logical". – Ignacio Vazquez-Abrams May 13 '11 at 09:30
  • 1
    It's not strictly bitwise. `(True | False) is True` whereas `(True | 0) is not True`. There are different overloadings of `|` in python 2.6 depending on whether the arguments are the special integral values `(True, False)` vs regular integral values. – Mike Samuel Mar 05 '12 at 18:14
  • 7
    What Mike said. More generally, it calls the `__or__` method of the first operand with the second operand, so you can define its behavior for your own classes. – Dawn Drescher May 26 '15 at 11:52
30

Yep, all answers above are correct.

Although you could find more exotic use cases for "|", if it is an overloaded operator used by a class, for example,

https://github.com/twitter/pycascading/wiki#pycascading

input = flow.source(Hfs(TextLine(), 'input_file.txt'))
output = flow.sink(Hfs(TextDelimited(), 'output_folder'))

input | map_replace(split_words, 'word') | group_by('word', native.count()) | output

In this specific use case pipe "|" operator can be better thought as a unix pipe operator. But I agree, bit-wise operator and union set operator are much more common use cases for "|" in Python.

Tagar
  • 13,911
  • 6
  • 95
  • 110
29

In Python 3.9 - PEP 584 - Add Union Operators To dict in the section titled Specification, the operator is explained. The pipe was enhanced to merge (union) dictionaries.

>>> d = {'spam': 1, 'eggs': 2, 'cheese': 3}
>>> e = {'cheese': 4, 'nut': 5}
>>> d | e
{'spam': 1, 'eggs': 2, 'cheese': 4, 'nut': 5} # comment 1
>>> e | d
{'cheese': 3, 'nut': 5, 'spam': 1, 'eggs': 2} # comment 2

comment 1 If a key appears in both operands, the last-seen value (i.e. that from the right-hand operand) wins --> 'cheese': 4 instead of 'cheese': 3

comment 2 cheese appears twice, the second value is selected so d[cheese]=3

BrokenBenchmark
  • 18,126
  • 7
  • 21
  • 33
user12989841
  • 301
  • 3
  • 3
  • I came across with an import statement from SqlAlchemy: S = Session | scoped_session. which suggest it is even works with classes as well – Gabor Jul 27 '22 at 08:31
14

Bitwise OR.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
7

It is a bitwise-or.

The documentation for all operators in Python can be found in the Index - Symbols page of the Python documentation.

aknosis
  • 3,602
  • 20
  • 33
tzot
  • 92,761
  • 29
  • 141
  • 204