Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where are the ampersand and vertical bar characters used in Python?

In the Wikipedia page describing short-circuit evaluation, & and | are listed as eager operators in Python. What does this mean and when are they used in the language?

like image 361
Yktula Avatar asked Jun 27 '11 05:06

Yktula


People also ask

What is ampersand used for in Python?

and is a Logical AND that returns True if both the operands are true whereas '&' is a bitwise operator in Python that acts on bits and performs bit by bit operation. Note: When an integer value is 0, it is considered as False otherwise True when using logically.

What is vertical bar in Python?

PythonServer Side ProgrammingProgramming. Vertical bar (|) stands for bitwise or operator. In case of two integer objects, it returns bitwise OR operation of two >>> a=4 >>> bin(a) '0b100' >>> b=5 >>> bin(b) '0b101' >>> a|b 5 >>> c=a|b >>> bin(c) '0b101' Pythonista. © Copyright 2022.

Is ampersand allowed in Python variable?

Which of the following is true for variable names in Python? a) unlimited length b) all private members must have leading and trailing underscores c) underscore and ampersand are the only two special characters allowed d) none of the mentioned Answer: a Explanation: Variable names can be of any length.

How do I print an ampersand in Python?

\n is also spaciel but if you want to actually see \n there is a specific string that can generate it. what about r"Hi&Hello"? @PawełKordowski: it's not Python that assigns meaning to the ampersand. Using a raw string literal (which is syntax, not an object type) will not make any difference here.


2 Answers

The wikipedia page is wrong, I've corrected it. | and & are not boolean operators, even though they are eager operators, which just means that they are not short circuit operators. As you probably know, here's how the python and and or operators work:

>>> def talk(x): ...     print "Evaluating: ", bool(x) ...     return x ...  >>> talk(1 == 1) or talk(2 == 1)   # 2 == 1 is not evaluated Evaluating:  True True >>> talk(1 == 1) and talk(2 == 1) Evaluating:  True Evaluating:  False False >>> talk(1 == 2) and talk(1 == 3)  # 1 == 3 is not evaluated Evaluating:  False False 

As far as I know, python has no eager boolean operators, they would have to be explicitly coded, for instance like this:

>>> def eager_or(a, b): ...    return a or b ... >>> eager_or(talk(1 == 1), talk(2 == 1)) Evaluating:  True Evaluating:  False True 

Now a and b are automatically evaluated when the function is called, even though or still short circuits.

As for the usage of | and &, when used with numbers, they are binary operators:

>>> bin(0b11110000 & 0b10101010) '0b10100000' >>> bin(0b11110000 | 0b10101010) '0b11111010' 

You're most likely to use | this way with python bindings to libraries that uses flags, like wxWidgets:

>>> frame = wx.Frame(title="My Frame", style=wx.MAXIMIZE | wx.STAY_ON_TOP) >>> bin(wx.MAXIMIZE) '0b10000000000000' >>> bin(wx.STAY_ON_TOP) '0b1000000000000000' >>> bin(wx.MAXIMIZE | wx.STAY_ON_TOP) '0b1010000000000000' 

When used with sets, they do the intersection and union operations, respectively:

>>> set("abcd") & set("cdef") set(['c', 'd']) >>> set("abcd") | set("cdef") set(['a', 'c', 'b', 'e', 'd', 'f']) 
like image 110
Lauritz V. Thaulow Avatar answered Oct 19 '22 08:10

Lauritz V. Thaulow


Something missing from the other answers here is that & and | don't have any universal meaning in Python; their meaning depends on the operands' types, using the magic __and__ and __or__ methods. Since these are methods, the operands are both evaluated (i.e. without short-circuiting) before being passed as arguments.

On bool values they are logical "and" and logical "or":

>>> True & False
False
>>> True | False
True
>>> bool.__and__(True, False)
False
>>> bool.__or__(True, False)
True

On int values they are bitwise "and" and bitwise "or":

>>> bin(0b1100 & 0b1010)
'0b1000'
>>> bin(0b1100 | 0b1010)
'0b1110'
>>> bin(int.__and__(0b1100, 0b1010))
'0b1000'
>>> bin(int.__or__(0b1100, 0b1010))
'0b1110'

On sets, they are intersection and union:

>>> {1, 2} & {1, 3}
{1}
>>> {1, 2} | {1, 3}
{1, 2, 3}
>>> set.__and__({1, 2}, {1, 3})
{1}
>>> set.__or__({1, 2}, {1, 3})
{1, 2, 3}

A couple of extra notes:

  • The __and__ and __or__ methods are always looked up on the class, not on the instance. So if you assign obj.__and__ = lambda x, y: ... then it's still obj.__class__.__and__ that's invoked.
  • The __rand__ and __ror__ methods on the class will take priority, if they are defined.

See the Python language reference for more details.

like image 27
kaya3 Avatar answered Oct 19 '22 09:10

kaya3