unintuitive for-loop behavior
Gregory Ewing
greg.ewing at canterbury.ac.nz
Sat Oct 1 07:33:30 EDT 2016
Steve D'Aprano wrote:
> # create a new binding
> x: address 1234 ----> [ box contains 999 ]
> x: address 5678 ----> [ a different box, containing 888 ]
In the context of CPython and nested functions, replace
"box" with "cell".
When I said "creating a new binding" I meant that the
name x refers to different cells at different times.
When I said "updating an existing binding" I meant that
the name x still refers to the same cell, but that cell
refers to a different object.
In a wider context, replace "box" with "slot in a
stack frame" or "slot in a namespace dictionary".
> But Python doesn't work that way! Variables aren't modelled by boxes in
> fixed locations, and there is no difference between "create a new binding"
> and "update an existing one".
There is very much a distintion. Each time you invoke
a function, a new set of bindings is created for all of
its parameters and local names. Assigning to those names
within the function, on the other hand, updates existing
bindings.
--
Greg
More information about the Python-list
mailing list