Is there a way to set up a global variable inside of a module? When I tried to do it the most obvious way as appears below, the Python interpreter said the variable __DBNAME__ did not exist.
...
__DBNAME__ = None
def initDB(name):
if not __DBNAME__:
__DBNAME__ = name
else:
raise RuntimeError("Database name has already been set.")
...
And after importing the module in a different file
...
import mymodule
mymodule.initDB('mydb.sqlite')
...
And the traceback was:
…
UnboundLocalError: local variable ‘DBNAME‘ referenced before assignment
…
Any ideas? I’m trying to set up a singleton by using a module, as per this fellow’s recommendation.
Here is what is going on.
First, the only global variables Python really has are module-scoped variables. You cannot make a variable that is truly global; all you can do is make a variable in a particular scope. (If you make a variable inside the Python interpreter, and then import other modules, your variable is in the outermost scope and thus global within your Python session.)
All you have to do to make a module-global variable is just assign to a name.
Imagine a file called foo.py, containing this single line:
Now imagine you import it.
However, let’s suppose you want to use one of your module-scope variables as a global inside a function, as in your example. Python’s default is to assume that function variables are local. You simply add a
globaldeclaration in your function, before you try to use the global.By the way, for this example, the simple
if not __DBNAME__test is adequate, because any string value other than an empty string will evaluate true, so any actual database name will evaluate true. But for variables that might contain a number value that might be 0, you can’t just sayif not variablename; in that case, you should explicitly test forNoneusing theisoperator. I modified the example to add an explicitNonetest. The explicit test forNoneis never wrong, so I default to using it.Finally, as others have noted on this page, two leading underscores signals to Python that you want the variable to be “private” to the module. If you ever do an
import * from mymodule, Python will not import names with two leading underscores into your name space. But if you just do a simpleimport mymoduleand then saydir(mymodule)you will see the “private” variables in the list, and if you explicitly refer tomymodule.__DBNAME__Python won’t care, it will just let you refer to it. The double leading underscores are a major clue to users of your module that you don’t want them rebinding that name to some value of their own.It is considered best practice in Python not to do
import *, but to minimize the coupling and maximize explicitness by either usingmymodule.somethingor by explicitly doing an import likefrom mymodule import something.EDIT: If, for some reason, you need to do something like this in a very old version of Python that doesn’t have the
globalkeyword, there is an easy workaround. Instead of setting a module global variable directly, use a mutable type at the module global level, and store your values inside it.In your functions, the global variable name will be read-only; you won’t be able to rebind the actual global variable name. (If you assign to that variable name inside your function it will only affect the local variable name inside the function.) But you can use that local variable name to access the actual global object, and store data inside it.
You can use a
listbut your code will be ugly:A
dictis better. But the most convenient is a class instance, and you can just use a trivial class:(You don’t really need to capitalize the database name variable.)
I like the syntactic sugar of just using
__m.dbnamerather than__m["DBNAME"]; it seems the most convenient solution in my opinion. But thedictsolution works fine also.With a
dictyou can use any hashable value as a key, but when you are happy with names that are valid identifiers, you can use a trivial class likeBoxin the above.