Globalize all variables in function without knowing names.

T

Tom

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?
 
D

Dennis Lee Bieber

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.

<Rest snipped>

Short answer: DON'T TRY

Longer answer:...

The user's data (variables, etc.) should NOT be part of the language
interpreter's name space. Consider what would happen if your interpreter
had a function called "parse"

def parse(instring):
pass

and now the user enters:

set parse 0

If you let user data into the interpreter name space, the above will
have wiped out your parse() function.

Instead, use a dictionary (defined at the module level)...

UserData = {}

so that

set parse 0

turns into

UserData["parse"] = 0

(well, obviously you'd have parsed the statement into something like
oper = "set"
target = "parse"
value = 0

and

if oper == "set":
UserData[target] = value


{heh... maybe some day I'll dig up my old copy of
http://isbndb.com/d/book/basex.html and code the language using Python
[the book was 8080 assembly, and the language had a syntax between
assembly and tiny-BASIC -- operator target value ; I also have the book
for K2FDOS, along with "The Source" [TRSDOS/LS-DOS source code, less the
M$ BASIC]}
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
T

Tom

Ah, thanks for that Dennis. I have a system already figured out where
the user manually pre-enters all the variables into a .py file, which
obviously isn't very efficient. The language isn't going to be used
much except by me, so I suppose I'll just work with that until I look
more into dictionaries, but I'm still beginning Python, and before
that the only coding I did was a little bit of C++, so I'll wait up
for that :p
 
S

Steven D'Aprano

Ah, thanks for that Dennis. I have a system already figured out where
the user manually pre-enters all the variables into a .py file, which
obviously isn't very efficient. The language isn't going to be used much
except by me, so I suppose I'll just work with that until I look more
into dictionaries, but I'm still beginning Python, and before that the
only coding I did was a little bit of C++, so I'll wait up for that :p

Dictionaries are fundamental to Python and very useful. Not learning
about them before starting to write code is like not learning about the
accelerator pedal before starting to drive a car. You might be able to
get somewhere by getting out and pushing, but it will be slow and
exhausting.

Have you done the tutorial?

http://www.python.org/doc/current/tutorial/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top