Python Global in One Line

To update a global variable in one line of Python, retrieve the global variable dictionary with the globals() function, and access the variable by passing the variable name as a string key such as globals()['variable']. Then overwrite the global variable using the equal symbol, for example in globals()['variable'] = 42 to overwrite the variable with value 42.

Let’s dive into this mission-critical challenge in greater detail!

Problem: The common approach to update a global variable takes two lines within the inner scope (in our example the function update()).

a = 42

def update():
  global a
  a = 21

# 21

Is this really necessary? Can’t you update the global variable a in only a single line of code?

You may want to look for something like this:

global a = 21

But this is incorrect syntax in Python. How to update the global variable in a single line?

Let’s dive into multiple methods to accomplish this. First, try them yourself in our interactive Python shell:

Exercise: Do all four ways update the global variable?

Method 1: Semicolon

def update_1():
    global a; a = 21

The semicolon allows you to write multiple Python expressions in a single line and executing them in sequence.

Related article: How to execute multiple lines of Python in a single line?

Method 2: sys.modules[__name__]

# Method 2
import sys
this = sys.modules[__name__]
this.a = 42

def update_2():
  this.a = 21

The code goes through the following steps:

  • Import the sys library.
  • Assign the module in which you run this code to the variable this.
  • Access the global variable a through the dot notation this.a.

By doing this, you work with qualified variable names (bare names) rather than implicit variable names. This reduces the likelihood of mistakes and is a clean way to solve this problem. This way, you can avoid using the global keyword altogether which can improve readability of your code.

Method 3: globals()[var]

def update_3():
    globals()['a'] = 21

The code first accesses all global variables using the globals() function that returns a dictionary mapping names to objects. You access the value associated to the key 'a'. The return value is the object to which global variable a points.

Method 4: Semicolon

def update_4():

This code is very similar to the previous code in Method 3. You use the dictionary update() instead of the indexing method though to overwrite the value of global variable a.

Where to Go From Here?

Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!