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
?
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
?
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.
5 = 101
, 7 = 111
, 101 | 111 = 111 = 7
–
Turenne or
, then? –
Kingpin True
or False
, and
and or
always return one of their operands, hence "coalescing" rather than "logical". –
Nephew (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. –
Pharisaic __or__
method of the first operand with the second operand, so you can define its behavior for your own classes. –
Sheri This is also the union set operator
set([1,2]) | set([2,3])
This will result in set([1, 2, 3])
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.
5 = 101
, 7 = 111
, 101 | 111 = 111 = 7
–
Turenne or
, then? –
Kingpin True
or False
, and
and or
always return one of their operands, hence "coalescing" rather than "logical". –
Nephew (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. –
Pharisaic __or__
method of the first operand with the second operand, so you can define its behavior for your own classes. –
Sheri 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
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.
It is a bitwise-or.
The documentation for all operators in Python can be found in the Index - Symbols page of the Python documentation.
Summarizing & extending previous answers:
The |
operator originally does bitwise OR. Here is a tutorial on bitwise operations.
f'{0b1100 | 0b1010 :04b}' == '1110'
On the other hand, Python operators can be overloaded by overloading their equivalent operator functions. In particular, |
can be overloaded by implementing the function __or__
(and __ror__
). Any Python library, standard or userbase, can overload any operator (e.g., archived PyCascading).
The Python built-in types such as set
and dict
overload |
to define union and merge, respectively. In particular, Python 3.9, PEP 584, brought the overloaded |
and other operators to dict
and other standard library types.
# sets; | does union of sets
# the order does not matter, i.e., the operation is commutative
{1, 2} | {2, 3} == {1, 2, 3}
{2, 3} | {1, 2} == {1, 2, 3}
# dicts; | does "merging" of dicts
# the order does matter, i.e., the operation is NOT commutative
{'a': 1, 'b': 2} | {'b': 99, 'c': 3} == {'a': 1, 'b': 99, 'c': 3}
{'b': 99, 'c': 3} | {'a': 1, 'b': 2} == {'a': 1, 'b': 2, 'c': 3}
Likewise, the types.UnionType
, introduced with Python 3.10, also overloads the binary-or operator |
, in this case to signify a union of types for type annotations/hints, e.g., typing.Union[int, str] == int | str
.
A recent, popular overloading and usage of the |
operator is in the Python LangChain library to chain/pipe operations. This is the exact code for how LangChain overloads the operator.
# LangChain chained sequence of components
chain = prompt | model | output_parser
Not specifically in function call but regarding OPs headline one might nowadays also list the pipe operator for "or" functionality in typing annotations!
See: https://docs.python.org/3/library/typing.html#typing.Union
For example:
def get_string_length(string: str | None = None) -> int:
if string is None:
string = 'muppets'
return len(string)
© 2022 - 2025 — McMap. All rights reserved.