30

I am trying to look for an easy way to define multiple global variables from inside a function or class.

The obvious option is to do the following:

global a,b,c,d,e,f,g......
a=1
b=2
c=3
d=4
.....

This is a simplified example but essentially I need to define dozens of global variables based on the input of a particular function. Ideally I would like to take care of defining the global name and value without having to update both the value and defining the global name independently.

To add some more clarity.

I am looking for the python equivalent of this (javascript):

var a = 1
  , b = 2
  , c = 3
  , d = 4
  , e = 5;
Stephen Gelardi
  • 485
  • 1
  • 5
  • 9
  • 4
    No, you don't need to do that. This is the entirely wrong way to do almost anything. You need to review lists, dictionaries, functions, and return values. – TigerhawkT3 Dec 06 '16 at 10:22
  • There is __very__ certainly a way to write your code without a single global. – bruno desthuilliers Dec 06 '16 at 10:57
  • 3
    @TigerhawkT3 I completely agree, the problem here is am editing a very poorly written project. To rewrite it though would be an extensive amount of effort, so I am stuck between a rock and a hard place and essentially just trying to come up with the be solution given my constraints. Which are defining values for global variables from within a function. – Stephen Gelardi Dec 06 '16 at 11:31
  • 9
    Is this valid python syntax? `global a,b,c,d,e,f,g......`. I'm looking for a terse way to declare a bunch of global variables as being global inside a function instead of the line by line declaration for each variable. – jxramos Apr 17 '17 at 21:06
  • 8
    @jxramos yes, it is valid syntax – Jodo Jun 08 '17 at 07:01

5 Answers5

47

You could just unpack the variables:

global x, y, z
x, y, z = 4, 5, 6
d3x
  • 655
  • 1
  • 5
  • 11
19

Alright, I'm new myself and this has been a big issue for me as well and I think it has to do with the phrasing of the question (I phrased it the same way when googling and couldn't quite find an answer that was relevant to what I was trying to do). Please, someone correct me if there's a reason I should not do this, but...

The best way to set up a list of global variables would be to set up a class for them in that module.

class globalBS():
    bsA = "F"
    bsB = "U"

now you can call them, change them, etc. in any other function by referencing them as such:

print(globalBS.bsA)

would print "F" in any function on that module. The reason for this is that python treats classes as modules, so this works out well for setting up a list of global variables- such as when trying to make an interface with multiple options such as I'm currently doing, without copying and pasting a huge list of globals in every function. I would just keep the class name as short as possible- maybe one or two characters instead of an entire word.

Hope that helps!

qui
  • 206
  • 2
  • 4
5

The globals() builtin function refers to the global variables in the current module. You can use it just like a normal dictionary

globals()['myvariable'] = 5
print(myvariable) # returns 5

Repeat this for all you variables.

However, you most certainly shouldn't do that

blue_note
  • 27,712
  • 9
  • 72
  • 90
3

If they are conceptually related one option is to have them in a dictionary:

global global_dictionary
global_dictionary = {'a': 1, 'b': 2, 'c': 3}

But as it was commented in your question, this is not the best approach to handle the problem that generated this question.

Martinbaste
  • 416
  • 2
  • 8
  • Firstly, globals are the wrong way to go. Secondly, dictionaries are mutable and can be mutated without being reassigned. – TigerhawkT3 Dec 06 '16 at 10:28
  • @Martinbaste, I considered this method, but then it requires editing all of the code that calls upon the global variables already.... – Stephen Gelardi Dec 06 '16 at 11:34
0
count = 1
globe = {}
while count < 27:
    yy = chr(96 + count)
    xx = count
    globe[yy] = xx
    count += 1
print((globe))
Michael
  • 1
  • 1
  • This will create a dictionary assigning the numbers to the letters. You can access by x = globe['a'].. – Michael Dec 15 '22 at 04:33
  • 1st, you should put the explanation as part of the answer, not in the comments. 2nd, using a dictionary was already given in this answer: https://stackoverflow.com/a/40993013/2745495. – Gino Mempin Dec 15 '22 at 04:36
  • I can't understand your editor. I try to explain but get error messages saying it expects code. As questioner requested, my example was the easiest way to assign numbers to all the letters. – Michael Dec 16 '22 at 04:15