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

Viewed 1933

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?

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.

Related