Does Python have the equivalent command of TCL? People who do not know, the "uplevel" command lets you run the code in the context of the caller. How can this be shown in Python:
def foo (): answer = 0 print "Answer", Answer # should be printed 0 times () Print is "Answer", Answer # Should be printed on 42 DEF bar (): Upleaval ("Answer = 42")
This is more than just specifying the variable, however, I am not looking for such a solution I want to be able to execute any code which only changes one dictionary.
In general, what you ask is not possible (with results you no doubt ). For example, imagine "any code" is x = 23
. Does it add a new variable x
to the local variable of your caller, thinking that you are searching for a black-magical way of executing this code in "Caller"? No, this will not happen - For important customizations made by the Python compiler once and for all, when def
implements, the exact set of local variables (all berms Is employed in the body of the function or otherwise compelled), and converts every access and setting of those bermains into indexing very rapidly in the stackframe. Having a exec ''
in the beginning of every possible caller can cause significant optimization - you can - and as a result the display of your system through the floor See crash).
To specify the caller's local barman,
exec thelocals in thecode, theglobals
broadly what you want, And you can inspect
module in the semi-appropriate way to local and globals of collars (as far as dark black magic - which I want to post to any colleague It may appear in the production code - can it ever be called "semi-appropriate" Finite praise can be awarded; -). But you specify "I want to be able to execute any code." And the only solution for is clear specification (and thanks for being so precise, because this makes the answer easy!): Then, use a different programming language.
Comments
Post a Comment