You can't change a Python function's local variables from outside

In an aside in a recent entry, I wrote:

With sufficient cleverness, you can construct a version of capture() that is passed store's name and puts the result in it directly, without needing you to make it an array and use store[0].

I'm wrong. In Python, no outside power can change a function's local variables (more precisely, no outside power can change the name bindings, doing the equivalent of 'localvar = something'). While you can make a version of capture() that sets global variables, setting a local variable in the function that called it is beyond its power.

Once I thought about it, this limitation is a logical consequence of how Python implements local variables. Since local variables aren't stored in a Python data structure, the CPython core would have to implement special functions to modify their values, which would only be useful for debuggers and people who want to commit dubious hacks.

(This does mean that not even Python debuggers can change local variables, although not all of them will clearly tell you this.)

These are my WanderingThoughts
(About the blog)

GettingAround
Full index of entries
Recent comments

This is part of CSpace, and is written by ChrisSiebenmann.

* * *

Atom feeds are available; see the bottom of most pages.

This is a DWiki.
(Help)

Categories: links, linux, programming, python, snark, solaris, spam, sysadmin, tech, unix, web

Search:
Written on 13 July 2007.
(Previous | Next)

Page tools: View Source, Add Comment.
Search:
Login: Password:
Atom Syndication: Recent Comments.

Last modified: Fri Jul 13 13:17:46 2007
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.