Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use more than one equal sign in a statement with the same variable?

Tags:

python

I ran into example code that uses a statement with the same variable:

event_type=event_type=str(payload_json['event'])

I have tried:

a=b=c=d=10

and all four variables (a, b, c and d) all become 10. Like:

a=10
b=10
c=10
d=10

This is an Amazon code example so I doubt my understanding of Python rather than the code example. The page can be found here: AWS Kinesis example

What is likely happening here? Some Python voodoo I don't understand or just a typo?

like image 561
Eric Snyder Avatar asked Nov 05 '17 01:11

Eric Snyder


People also ask

Why are there double equal signs?

In programming languages == sign or double equal sign means we are comparing right side with left side. And this comparison returns true or false. We usually use this comparison inside if condition to do something specific. Double equal operator is a very common used operator after single equal.

Why do we use 2 equal signs in Python?

The two equal signs are to denote that the the variable is exactly equal to the comparator for example if we say in simple words. 1 == 1 # This means that the number one is exactly equal to the number one. And in your case.

What does a double equal sign mean in code?

Equality between two variables/values (==) Equality is represented in the program using the DOUBLE EQUAL signs operator. The Equality operator (==) should not be confused with the assignment operator.

What does 2 equal signs mean in Java?

Notice that ONE equal sign is used to "assign" a value, but TWO equal signs are used to check to see if numerical values are equal to one another. Relational operators always yield a true or false result.

What does two equals signs mean in R?

The Equality Operator == Relational operators, or comparators, are operators which help us see how one R object relates to another. For example, you can check whether two objects are equal (equality) by using a double equals sign == .

What does the equal sign mean in coding?

In programming, the equals sign (=) is used for equality and copying. For example, if x = 0 means "if X is equal to zero;" however x = 0 means "copy the value zero into the variable X." Double equals signs (==) means equals to in C. For example, if (x == 0) means if X is equal to zero.


1 Answers

a = a = b is always equivalent to a = b in python. Using statements with multiple equal signs as you describe is called chained assignment and is supported in many programming languages. Some languages will raise an error upon detecting chained assignment of the same variable (C), but others simply ignore it (python, javascript).

It would be a bad idea to change this behavior, and not easy to achieve because the assignment operator's behavior is built in to python with no modifcation hooks provided (see: Is it possible to overload Python assignment?). Thus I think it is safe to assume that this is a (harmless) typo you have uncovered.

like image 95
7yl4r Avatar answered Oct 21 '22 03:10

7yl4r