9

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?

Eric Snyder
  • 1,372
  • 2
  • 15
  • 31
  • 1
    You seem to have answered yourself. Yes this is for assigning same value to multiple variables. – Nabin Nov 05 '17 at 01:29
  • 4
    @Fan_of_Martijn_Pieters: You have missed the question. The actual question is about code of the form `a=a=value`, where the object being assigned to is duplicated. The OP discusses `a=b=c=d=value` only as an exploration of what compound assignments statements do. – Eric Postpischil Nov 05 '17 at 01:30
  • 1
    I think `a=a=b` is no different from `a=b`. It just looks like a mistake to me. – Tom Karzes Nov 05 '17 at 01:34
  • @EricPostpischil This thread should be closed. Not sure why you think it should be answered. – Nabin Nov 05 '17 at 01:36
  • 2
    Something like `a = a = 10` strikes me as an artefact of copying and pasting, it is semantically equivalent to and less redundant than the intended `a = 10`. – juanpa.arrivillaga Nov 05 '17 at 01:36
  • 5
    @Fan_of_Martijn_Pieters: It is a clear question about the meaning of specific code. I presume the vote to close as unclear is yours, but there is nothing unclear about the question. – Eric Postpischil Nov 05 '17 at 01:38
  • I do not believe a thread should exist for a typo from some documentation – Nabin Nov 05 '17 at 02:06
  • Sounds like there is no reason to do a=a=10 then...typo. That is what I was wanting to find out. – Eric Snyder Nov 05 '17 at 02:10
  • @Fan_of_Martijn_Pieters: “What the hell was the author thinking?” is a vexing question that arises not infrequently when examining other people’s code. Often that comes down to a question of whether the author knew something about the code that the reader does not or whether the author made a mistake. Sometimes there is some subtle or obscure behavior being used, so we cannot immediately decide. If we had an oracle that told us “This is just a mistake in the code; it is okay to fix it,” many hours of labor would have been saved. Understanding when code is mistaken is valuable information. – Eric Postpischil Nov 05 '17 at 03:16

1 Answers1

2

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.

7yl4r
  • 3,391
  • 2
  • 32
  • 43
  • 2
    I suggest you remove or qualify the comment that these are equivalent in many other languages. In C, for example, `a = b` assigns the value of `b` to `a` (converted as necessary and appropriate), but `a = a = b` has undefined behavior (because it is illegal to modify an object twice between sequence points). – Eric Postpischil Nov 05 '17 at 02:33
  • thanks @EricPostpischil . I've updated the paragraph to be more accurate. – 7yl4r Nov 06 '17 at 21:34