Skip to content
Advertisement

Multiple assignment semantics

In Python one can do:

JavaScript

I checked the generated bytecode using dis and they are identical.
So why allow this at all? Would I ever need one of these instead of the others?

Advertisement

Answer

One case when you need to include more structure on the left hand side of the assignment is when you’re asking Python unpack a slightly more complicated sequence. E.g.:

JavaScript

This has proved useful for me in the past, for example, when using enumerate to iterate over a sequence of 2-tuples. Something like:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement