I agree with the comments that this is somewhat bad practice, and also that the two are sufficiently different that this doesn't entirely make sense, but we can attempt the best analog. Python doesn't use the same "workspace" terminology (and some things likely don't cross over neatly), but from the docs it would seem that assignin
is used largely to assign values to variables which aren't currently in-scope by accessing either the "base"
scope (global), or other function scopes. In python you can (within a function) indicate that you want to use the global version of a variable with the global
keyword, then simply assign a value to it as normal: global myvar
then myvar = "some_value"
. If you have both a global version as well as one of the same name in an enclosing scope, and you want to access one level higher, but not necessarily the global scope you instead use the keyword nonlocal
.
The other thing this function does is allow you to create variable names programmatically which is generally considered bad practice, as your variable names should generally be fixed, and not variable themselves. In this instance, it would seem the code may be replicating the functionality of saving all the variables from an interactive session, then re-loading them later in a new interactive session. In this case the general wisdom of not having "variable variables" is less applicable, and indeed makes sense over the general solution to "variable variables" which is a dictionary (or other data structure). The actual solution to creating new variables programmatically is the eval
function, which for many reasons is generally avoided, but particularly for interacting with information from a live coding session may be appropriate. In this case you would build a string representing the appropriate python expression such as:
eval(str(vars[i]) + "=" + str(newData1[vars[i]]))
*note you will have to spend some time to figure out how to make sure you're building the correct expression for eval
this is a very rough example.