Search code examples
pythonmysqlglobal-variables

Setting database variables as global variables: Python


I am trying to figure out global variables in python, and while there are many forum posts and examples, none of them really match what I need. Here is the problem: I am using a MySQL database to receive 11 different variables. These variables are used for control purposes, such as the opening and closing of valves at specific times and days. I need these variables to be global and they must be able to be used in many other files. At the moment, these variables only exist within the file that reads the database and assigns the variables a value.

Here's my question:

When I import the variables from the database, can I simply assign them to be global in that script? Like this:

cursor.execute ("select variable1,variable2,....variableN from transaction where TransactionNumber=(select MAX(TransactionNumber) from transaction)")

readfromdb= cursor.fetchone() 

variable1,variable2,.....variableN=list(readfromdb)

global variable1,variable2...variableN. 

And then import this file in each other file I'm using?

I have tried doing this method, but receive the following error:

SyntaxError: name 'TransactionNumber' is assigned to before global declaration

I am open to other suggestions to make this process more efficient/streamlined. But my only requirement is that the script that is currently reading the database MUST not change. It is a fundamental requirement that it continues reading and assigning the database variables. I won't bore you with details, but that script is vital to checking the data we receive from the database and making sure that it is correctly formatted for use since we will be receiving data from another computer.


Solution

  • The message has your answer in it. Just move the global statement above the first use of any of the variables.

    You'll have to repeat those global declarations at the top of any function that needs to assign new values to any of those names.

    Juanpa Arrivillaga makes and excellent point. To share across module boundaries, you'll need to use module names to specify exactly which module they are defined in. The global statement really only helps share names among functions and initialization code in a single module.

    If "source1.py" creates a module-level ("global") variable or function name1, and you want to use it in "source2.py", you'll have to import source1 in the source2.py file and then refer to the variable/function as `source1.name1'.

    At this point, I really recommend looking at a dictionary or some type of custom class or namedtuple object. You can solve this with globals, but it's going to end up looking like a dog walking on two legs. It's impressive not because the dog does it well, but because the dog does it at all.

    I'd use either an object, dictionary or named tuple to hold those fields, so that the whole mess could be passed as an argument to functions. Global variables get messy, particularly in code that gets updated frequently. This gets rid of all of those global statements. The class type for an object or the field list for a named tuple becomes a single point of definition that the whole module can see.