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.
-
Nice try, but when you make the post CW, all the responses are CW as well. – Paul Tomblin Jan 06 '09 at 17:21
-
The community wiki checkbox is marked by default, you can deselect it to have a non community wiki answer – Vinko Vrsalovic Jan 06 '09 at 17:23
-
1This just shouldn't be a CW post. It's not a "real" question because Vinko probably already knows the answer. But it's still real in that it's about programming and people can google for it and such. – Kenan Banks Jan 06 '09 at 17:29
-
Yes, that's why I didn't want to let your answer go to waste – Vinko Vrsalovic Jan 06 '09 at 17:38
-
1Thanks. I was a little annoyed, honestly, that a decent answer would go to waste because a question was improperly phrased. Interesting, the sort of problems that crop up as we trailblaze the wiki Q&A space. – Kenan Banks Jan 06 '09 at 17:44
-
possible duplicate of [Pipe character in Python](http://stackoverflow.com/questions/5988665/pipe-character-in-python) – AncientSwordRage Jan 22 '15 at 10:36
-
@Pureferret: this is a more general question than the other, which is only about us as bitwise OR operator. – e100 Mar 26 '15 at 18:38
3 Answers
In Python, the '|'
operator is defined by default on integer types and set types.
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.
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.

- 207,056
- 34
- 155
- 173
Bitwise OR
It could also be "tricked" into a pipe like in unix shells, see here http://code.google.com/p/python-pipeline/

- 17,098
- 11
- 52
- 68