Ray
Ray

Reputation: 131

How does Python process a statement with both assignment and a comparison?

I've got the following line:

group_index = apps["special_groups"] == group

From my understanding, group_index is being assigned the value in apps["special_groups"]. Then I see the == operator, but what does it do with the result? Or is it comparing apps["special_groups"] to group first?

Upvotes: 0

Views: 213

Answers (3)

dpdwilson
dpdwilson

Reputation: 1007

From the Python docs, Section 5.14:

Python evaluates expressions from left to right. Notice that while evaluating an assignment, the right-hand side is evaluated before the left-hand side.

Therefore it evaluates the right-hand side, apps["special_groups"] == group, first, and then it assigns this result to the left-hand side, group_index.

Upvotes: 1

Barmar
Barmar

Reputation: 781088

From the Python Evaluation Order documentation:

Notice that while evaluating an assignment, the right-hand side is evaluated before the left-hand side.

So it first evaluates apps["special_groups"] == group, then assigns the result of this to group_index.

Upvotes: 4

mpcabd
mpcabd

Reputation: 1807

It assigns the value of the boolean comparison [True or False] to the LHS variable group_index.

Upvotes: 0

Related Questions