What's wrong with this code?

Ian Kelly ian.g.kelly at gmail.com
Tue Jul 24 10:26:28 CEST 2012


On Mon, Jul 23, 2012 at 7:19 PM, Andrew Cooper <amc96 at cam.ac.uk> wrote:
> Python is a statically scoped language, whereas the functionality you
> are expecting would be an example of dynamically scoped.

While it's true that Python is statically scoped, that has nothing at
all to do with the code from the OP's question, which contains only
one (global) scope anyway.

The real issue is confusion between name binding and object mutation.
By reassigning c and d, the OP is evidently trying to mutate the list
named x (or to be more precise, the list that is the second element of
the list named x).  But this doesn't work, because name-binding
doesn't mutate objects as it does in languages with variable semantics
(C, for example); it merely rebinds the names to different objects.  c
and d end up bound to values that weren't in the list to begin with,
meanwhile the list remains unchanged and e and f are still just bound
to None.



More information about the Python-list mailing list