-2

I just saw an expression like 1 and "b" in python code and even 1 and "a" or "b".

I checked the help for int and figured out that and means bitwise and with two integers

__and__(...)
 |      x.__and__(y) <==> x&y

but there's no mention of a str as second operand. I assume that there's no way that the str can be interpreted as int because there's just no way of conversion or casting and that I'm thus missing a meaning of and.

Then I started experimenting to get the first expression above to be 1, but no success.

Kalle Richter
  • 8,008
  • 26
  • 77
  • 177

1 Answers1

1

__and__ is used for the bitwise operator &. and is a logical, boolean operator, and is not handled by a special method, because it short-circuits (if the first expression evaluates to false, the second expression is not even executed).

You simply have a boolean expression, and the standard truth value testing rules apply here.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343