Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pipe character in Python

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?

like image 593
alwbtc Avatar asked May 13 '11 07:05

alwbtc


People also ask

Is there a pipe operator in Python?

Pipe is a beautiful package that takes Python's ability to handle data to the next level. It takes a SQL-like declarative approach to manipulate elements in a collection. It could filter, transform, sort, remove duplicates, perform group by operations, and a lot more without needing to write a gazillion lines of code.

What is |= mean in Python?

|= on Booleans The Python |= operator when applied to two Boolean values A and B performs the logical OR operation A | B and assigns the result to the first operand A . As a result, operand A is False if both A and B are False and True otherwise.

What is pipe symbol in programming?

In programming, the double pipe "||" is used to represent an OR boolean operator.

What is double pipe in Python?

The double pipe "||" is a logical or, and can be used in logical statements, like "x == 0 || x == 1". Here's an example of what the bitwise or does: if a=0101 and b=0011, then a|b=0111.


6 Answers

This is also the union set operator

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

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

like image 88
gui11aume Avatar answered Oct 04 '22 06:10

gui11aume


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.

like image 39
ahmet alp balkan Avatar answered Oct 04 '22 06:10

ahmet alp balkan


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.

like image 25
Tagar Avatar answered Oct 04 '22 05:10

Tagar


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

like image 24
user12989841 Avatar answered Oct 04 '22 05:10

user12989841


Bitwise OR.

like image 41
Ignacio Vazquez-Abrams Avatar answered Oct 04 '22 05:10

Ignacio Vazquez-Abrams


It is a bitwise-or.

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

like image 23
tzot Avatar answered Oct 04 '22 07:10

tzot