Globalize all variables in function without knowing names.

Tom Tom.sully at gmx.com
Sun Jul 26 15:45:10 EDT 2009


Does anyknow know how to do this? The reason I'm asking is because I'm
trying to make a mini-programming language for fun, and need to work
with variables.

The user types 'set NAME "DATA" ' or whatever else the variable is
going to be called/contain.

I have:
def variablework(varname, varset):
    exec("global "+varname) #DEFUNCT
    vars()[varname] = varset
    exec("print "+varname)   #Debug purposes only

This sets the variable (varname is the name of the variable, and
varset is the data for it to contain) all fine and dandy, but it is
local, and after some googling I realised that you can't globalised
variables using exec. So I think the next best thing would be to find
a way to globalize every variable in the function, but I don't know
how to do this. If there isn't a way of doing this, can someone
suggest another way of globalizing variable without knowing their
names?



More information about the Python-list mailing list