39

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;
5
  • 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. Commented Dec 6, 2016 at 10:22
  • There is very certainly a way to write your code without a single global. Commented Dec 6, 2016 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. Commented Dec 6, 2016 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. Commented Apr 17, 2017 at 21:06
  • 8
    @jxramos yes, it is valid syntax Commented Jun 8, 2017 at 7:01

5 Answers 5

53

You could just unpack the variables:

global x, y, z
x, y, z = 4, 5, 6
Sign up to request clarification or add additional context in comments.

Comments

21

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!

1 Comment

Brilliant idea to use a Class variable to deal situation where global variable use is unavoidable.
6

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

2 Comments

globals() is probably an even worse idea than using global.
how could he use global during runtime to define variables with unknown names and values coming from user input??
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.

2 Comments

Firstly, globals are the wrong way to go. Secondly, dictionaries are mutable and can be mutated without being reassigned.
@Martinbaste, I considered this method, but then it requires editing all of the code that calls upon the global variables already....
0
count = 1
globe = {}
while count < 27:
    yy = chr(96 + count)
    xx = count
    globe[yy] = xx
    count += 1
print((globe))

3 Comments

This will create a dictionary assigning the numbers to the letters. You can access by x = globe['a']..
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: stackoverflow.com/a/40993013/2745495.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.