Puzzle 27: Explanation
Explore how Python executes augmented assignments such as += by distinguishing between rebinding and in-place mutation. Understand special methods like __iadd__ and why functional programming avoids mutating passed objects. This lesson helps you clarify Python's behavior with lists and ranges and improve your coding approach.
We'll cover the following...
We'll cover the following...
Try it yourself
Try executing the code below to verify the results:
In Puzzle 16: Call Me Maybe, we talked about rebinding versus mutation. Most of the time, items += range(n) is translated to items = items + range(n), a process which is called rebinding.
In some cases, there is a special optimization for +=. Here’s a more detailed explanation from the Python ...