I'm going through my companies code, and there's instances where someone is assigning some variable x to another variable y, followed by == "true".
I'm not following and google is failing me. Do I set x to equal y if y equals "true"? There's no conditional statements (if) and I've never encountered this before. I don't even know how to search for this.
If it was just x = y = z, I would assume that both x and y are being set to the value of z. but a == outside of a conditional is throwing me
CodePudding user response:
It is more obvious if you add parentheses according to operator precedence:
x = (y == "true")
y == "true"
is an expression that evalutates to a bool
, so it will be True
or False
. That value is then assigned to x
.
Or in more words:
if y == "true":
x = True
else:
x = False
CodePudding user response:
x = something
assigns the value "something
" to x
. y == something
evaluates to True
if the value of y
is equal to "something
", and evaluates to False
if y
is not equal to "something
".
So, x = y == "true"
means "set x
to True
if y
is equal to the string "true"
. Otherwise set x
to False
".