This question originally asked (wrongly) what does "|" mean in Python, when the actual question was about Django. That question had a wonderful answer by Triptych I want to preserve.
这个问题最初问(错误地)是什么“|”在Python中,当实际问题是关于Django时。这个问题得到了我希望保留的Triptych的精彩答案。
3 个解决方案
#1
In Python, the '|'
operator is defined by default on integer types and set types.
在Python中,'|'默认情况下,在整数类型和集类型上定义运算符。
If the two operands are integers, then it will perform a bitwise or, which is a mathematical operation.
如果两个操作数是整数,那么它将执行按位或,这是一个数学运算。
If the two operands are set
types, the '|'
operator will return the union of two sets.
如果两个操作数都是设置类型,则为'|'运算符将返回两组的并集。
a = set([1,2,3])
b = set([2,3,4])
c = a|b # = set([1,2,3,4])
Additionally, authors may define operator behavior for custom types, so if something.property
is a user-defined object, you should check that class definition for an __or__()
method, which will then define the behavior in your code sample.
此外,作者可以为自定义类型定义操作符行为,因此如果something.property是用户定义的对象,则应检查__或__()方法的类定义,然后定义代码示例中的行为。
So, it's impossible to give you a precise answer without knowing the data types for the two operands, but usually it will be a bitwise or.
因此,如果不知道两个操作数的数据类型,就不可能给出一个精确的答案,但通常它会是一个按位或者。
#2
Bitwise OR
#3
It could also be "tricked" into a pipe like in unix shells, see here http://code.google.com/p/python-pipeline/
它也可能被“欺骗”到像unix shell中的管道,请参见http://code.google.com/p/python-pipeline/
#1
In Python, the '|'
operator is defined by default on integer types and set types.
在Python中,'|'默认情况下,在整数类型和集类型上定义运算符。
If the two operands are integers, then it will perform a bitwise or, which is a mathematical operation.
如果两个操作数是整数,那么它将执行按位或,这是一个数学运算。
If the two operands are set
types, the '|'
operator will return the union of two sets.
如果两个操作数都是设置类型,则为'|'运算符将返回两组的并集。
a = set([1,2,3])
b = set([2,3,4])
c = a|b # = set([1,2,3,4])
Additionally, authors may define operator behavior for custom types, so if something.property
is a user-defined object, you should check that class definition for an __or__()
method, which will then define the behavior in your code sample.
此外,作者可以为自定义类型定义操作符行为,因此如果something.property是用户定义的对象,则应检查__或__()方法的类定义,然后定义代码示例中的行为。
So, it's impossible to give you a precise answer without knowing the data types for the two operands, but usually it will be a bitwise or.
因此,如果不知道两个操作数的数据类型,就不可能给出一个精确的答案,但通常它会是一个按位或者。
#2
Bitwise OR
#3
It could also be "tricked" into a pipe like in unix shells, see here http://code.google.com/p/python-pipeline/
它也可能被“欺骗”到像unix shell中的管道,请参见http://code.google.com/p/python-pipeline/