Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This isn't possible. I think this is to allow for performance optimizations later on. Python bytecode references locals by index, not by name; if locals() was required to be writable, it could prevent interpreters from implementing some optimizations, or make them more difficult.</p> <p>I'm fairly certain you're not going to find any core API that guarantees you can edit locals like this, because if that API could do it, locals() wouldn't have this restriction either.</p> <p>Don't forget that all locals must exist at compile-time; if you reference a name that isn't bound to a local at compile-time, the compiler assumes it's a global. You can't "create" locals after compilation.</p> <p>See <a href="https://stackoverflow.com/questions/1360721/how-to-get-set-local-variables-of-a-function-from-outside-in-python/1361058#1361058">this question</a> for one possible solution, but it's a serious hack and you really don't want to do that.</p> <p>Note that there's a basic problem with your example code:</p> <pre><code>@depends("a", "b", "c", "d", "e", "f") def test(): put_into_locals(test.dependencies) </code></pre> <p><code>"test.dependencies"</code> isn't referring to "f.dependencies" where f is the current function; it's referencing the actual global value "test". That means if you use more than one decorator:</p> <pre><code>@memoize @depends("a", "b", "c", "d", "e", "f") def test(): put_into_locals(test.dependencies) </code></pre> <p>it'll no longer work, since "test" is memoize's wrapped function, not depends's. Python <em>really</em> needs a way to refer to "the currently-executing function" (and class).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload