Python - Easiest way to define multiple global variables

Stephen Gelardi picture Stephen Gelardi · Dec 6, 2016 · Viewed 42.7k times · Source

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;

Answer

d3x picture d3x · Aug 7, 2018

You could just unpack the variables:

global x, y, z
x, y, z = 4, 5, 6