Pipe character in Python

Pipe character in Python

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.

Pipe character in Python

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.

Leave a Reply

Your email address will not be published. Required fields are marked *