1

I am using Python 3 and am trying to understand some code that uses "\" and "|", but not in the context of strings, where I usually see "\". I can't find the documentation in the manual that explains this:

def test(this, that):
     return isinstance(that, something) and \
          self.number == that.number and \
          self.stuff = that.stuff

also |

this.stuff = self.things | something.stuff

Thank you

  • 7
    `\ ` is a line continuation marker. `|` is bitwise OR. – N Chauhan Jan 01 '20 at 16:29
  • 1
    See https://stackoverflow.com/questions/53162/how-can-i-do-a-line-break-line-continuation-in-python – mkrieger1 Jan 01 '20 at 16:45
  • 1
    And https://stackoverflow.com/questions/1746613/bitwise-operation-and-usage – mkrieger1 Jan 01 '20 at 16:50
  • 1
    Explained here: https://docs.python.org/3/reference/lexical_analysis.html#explicit-line-joining and here: https://docs.python.org/3/reference/expressions.html#binary-bitwise-operations – mkrieger1 Jan 01 '20 at 16:54

2 Answers2

2

Backslashes are used for line-continuation. So this:

def test(this, that):
     return isinstance(that, something) and \
          self.number == that.number and \
          self.stuff = that.stuff

Is equivalent to this:

def test(this, that):
     return isinstance(that, something) and self.number == that.number and self.stuff = that.stuff

In other words, it's purely for the developer benefit, no practical implication for the code. PEP I think recommends 80 character line width max so use it if you're over that. I should note as well though that you can get away without using \ if you use parenthesis:

def test(this, that):
     return (isinstance(that, something) and 
          self.number == that.number and 
          self.stuff = that.stuff
)

The pipe '|' is a bitwise 'or' operator. It is a frequently asked question, so there is more info here: https://wiki.python.org/moin/BitwiseOperators

Neil
  • 3,020
  • 4
  • 25
  • 48
  • thanks, that is working great, but I still doesn't understand why: `test = 5 | 67` `print(test)` shows 71. – user2283474 Jan 01 '20 at 16:42
  • 1
    run "bin(5)" and "bin(67)". You will see the binary representation of 5 and 67. Then do a bitwise "or" on each position. Convert the result back to base 10, and you will see it is 71. If you don't know what binary is or bitwise operations, maybe read up on that or open a new question about it. For almost all use cases you won't be doing bitwise operations, and can just use Python's "or". – Neil Jan 01 '20 at 16:50
  • It isn't doing "or" on 5 and 67. It's doing it on 1000011 and 101. And it's doing it one bit at a time. – Neil Jan 01 '20 at 16:51
  • What result were you expecting? Maybe if I know that I can tell you how to achieve it. – Neil Jan 01 '20 at 16:54
  • ok, I see that `test = 5 | 67` is doing something bitwise. `bin(5)` gives me 0b101 and `bin(67)` gives me 0b10000011. Doing the | operation gives me 0b1000111 which is `bin(71)`. I'm guessing it compares on the far right, there is a 1 in either of those, so it is true, again to the next on the left and again which gives me essentially 011 or 111 = 111. Correct? – user2283474 Jan 01 '20 at 17:04
  • 1
    yes that is correct. Negative numbers are a bit more tricky but for positive numbers that's how it works. – Neil Jan 01 '20 at 17:45
-2

'\' means continue on next line
'|' uses as OR operator

mkrieger1
  • 19,194
  • 5
  • 54
  • 65