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?
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.
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.
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.
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.
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 == .
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With