<p>我正在开发一个Python模块,它充当一种旧的游戏内脚本语言的解释器。我发现我需要很多变量,我的模块顶部如下所示:</p>
<pre><code>from mufdatatypes import *
debugline=0
code=[]
debugline=0 #Line where something interesting starts
currentline=0 #Line where we currently are
infunc=0 #Need to know if we're in a function or not.
iflevel=0 #Need to track how far into ifs we are
inelse=0 #If we're in an if, have we already done an else?
looplevel=0 #Also need to know how many loops deep we're in
instrnum=0
charnum=0
gvarlist=[ "me": mufvariable(dbref(2)) , "loc" : mufvariable(dbref(0)) , "trigger" : mufvariable(dbref(-1)) , "command": mufvariable("") ]
item=''
structstack=[]
</code></pre>
<p>这真是乱七八糟。在执行大部分定义的函数的开头,它看起来是这样的:</p>
<pre><code>def mufcompile(rawcode, primstable):
"""Compiles a source string into a list of instructions"""
global source
global code
global debugline
global currentline
global code #The actual instructions will be stored here.
global infunc #Need to know if we're in a function or not.
global iflevel #Need to track how far into ifs we are.
global inelse #If we're in an if, have we already done an else?
global looplevel #Also need to know how man loops deep we're in.
global addresstable #This will hold a table of function addresses.
global structstack #This list stack will be used to hold status info as we go further down complex statements
global instrnum #Number of instruction. Important for moving the EIP.
global vartable #Table of variables. Dictionary.
global charnum #Character in the line we're at.
global item
</code></pre>
<p>我有一种感觉,我这样做是不正确的,也许,有趣的人谁真正知道他们在做什么与Python。我知道变量可以当场声明,但如果我正在编写的函数上面有任何函数引用这些变量,它们就不会编译,对吗?我也有点担心全球化的程度。导入此模块的模块是否可以访问这些变量?我不想让他们这么做。你知道吗</p>