If you want to simply access a global variable you just use its name. However to change its value you need to use the global keyword.

E.g.

global someVar
someVar = 55

This would change the value of the global variable to 55. Otherwise it would just assign 55 to a local variable.

The order of function definition listings doesn't matter (assuming they don't refer to each other in some way), the order they are called does.

Answer from Levon on Stack Overflow
Top answer
1 of 6
549

If you want to simply access a global variable you just use its name. However to change its value you need to use the global keyword.

E.g.

global someVar
someVar = 55

This would change the value of the global variable to 55. Otherwise it would just assign 55 to a local variable.

The order of function definition listings doesn't matter (assuming they don't refer to each other in some way), the order they are called does.

2 of 6
150

Within a Python scope, any assignment to a variable not already declared within that scope creates a new local variable unless that variable is declared earlier in the function as referring to a globally scoped variable with the keyword global.

Let's look at a modified version of your pseudocode to see what happens:

# Here, we're creating a variable 'x', in the __main__ scope.
x = 'None!'

def func_A():
  # The below declaration lets the function know that we
  #  mean the global 'x' when we refer to that variable, not
  #  any local one

  global x
  x = 'A'
  return x

def func_B():
  # Here, we are somewhat mislead.  We're actually involving two different
  #  variables named 'x'.  One is local to func_B, the other is global.

  # By calling func_A(), we do two things: we're reassigning the value
  #  of the GLOBAL x as part of func_A, and then taking that same value
  #  since it's returned by func_A, and assigning it to a LOCAL variable
  #  named 'x'.     
  x = func_A() # look at this as: x_local = func_A()

  # Here, we're assigning the value of 'B' to the LOCAL x.
  x = 'B' # look at this as: x_local = 'B'

  return x # look at this as: return x_local

In fact, you could rewrite all of func_B with the variable named x_local and it would work identically.

The order matters only as far as the order in which your functions do operations that change the value of the global x. Thus in our example, order doesn't matter, since func_B calls func_A. In this example, order does matter:

def a():
  global foo
  foo = 'A'

def b():
  global foo
  foo = 'B'

b()
a()
print foo
# prints 'A' because a() was the last function to modify 'foo'.

Note that global is only required to modify global objects. You can still access them from within a function without declaring global. Thus, we have:

x = 5

def access_only():
  return x
  # This returns whatever the global value of 'x' is

def modify():
  global x
  x = 'modified'
  return x
  # This function makes the global 'x' equal to 'modified', and then returns that value

def create_locally():
  x = 'local!'
  return x
  # This function creates a new local variable named 'x', and sets it as 'local',
  #  and returns that.  The global 'x' is untouched.

Note the difference between create_locally and access_only -- access_only is accessing the global x despite not calling global, and even though create_locally doesn't use global either, it creates a local copy since it's assigning a value.

The confusion here is why you shouldn't use global variables.

🌐
Reddit
reddit.com › r/learnpython › how can i update a global variable with a variable from a function?
r/learnpython on Reddit: How can I update a global variable with a variable from a function?
November 6, 2021 -

I created two variables at the beginning of a script: price1 and price2

I also have a function called get_price() that will create variables: price_a and price_b. I need these function variables to update the variables from the beginning of the script.

Here is a snippet:

price1 = 0
price2 = 0

async def get_price(url):
    LOTS_OF_CODE_HERE
    return price

async def main():
    task1 = asyncio.create_task(get_price(url1))
    task2 = asyncio.create_task(get_price(url2))
    price_a = await task1
    price_b = await task2
    print(f'price_a: {price_a}')
    print(f'price_b: {price_b}')
    price1 = price_a
    price2 = price_b

asyncio.run(main())

print(f'price1: {price1}')
print(f'price2: {price2}')

expected output:

price_a: 4000
price_b: 5000
price1: 4000
price2: 5000

actual output:

price_a: 4000
price_b: 5000
price1: 0
price2: 0

What am I doing wrong here?

Discussions

How to handle 'global' variables?
Hi, Newby question here. I’m creating a program that traverses a directory tree using os.walk. Each directory in that tree is checked for a distinct text file, the text file is opened, and the contents searched for image filenames & urls. I want to keep track of the total number of text files ... More on discuss.python.org
🌐 discuss.python.org
0
May 8, 2024
Can't use global variables within a function
Today i’m trying out functions, but i’ve run in a small (inconsistancy?) issue. Mainly i’m trying to print out global variables within a function, which works, but when I try to change the value of the pre-created variables, from inside the function, its not allowing me. More on discuss.python.org
🌐 discuss.python.org
1
November 19, 2021
Modifying variable outside of function
Think its a beginner question, but I do not understand why # some lines of working code minval = 10 maxval =22 test = 0 # a new added variable def a_function(): # some lines of working code test +=1 # new code line # some lines of working code # more lines of working a_function() # more working ... More on discuss.python.org
🌐 discuss.python.org
0
December 2, 2024
How to make variables global by default, Python 3.11
I have Python 3.11 on Windows 10 Pro. I’m still a bit new to Python but I’m learning. In Python 3.11 the variables declared in the main program are not global by default and I cannot use them in functions. So if I want to use them in a function I have to use the global prefix/keyword. More on discuss.python.org
🌐 discuss.python.org
0
June 10, 2024
🌐
W3Schools
w3schools.com › python › python_variables_global.asp
Python - Global Variables
To change the value of a global variable inside a function, refer to the variable by using the global keyword: x = "awesome" def myfunc(): global x x = "fantastic" myfunc() print("Python is " + x) Try it Yourself »
🌐
Real Python
realpython.com › python-use-global-variable-in-function
Using and Creating Global Variables in Your Python Functions – Real Python
December 8, 2024 - Inside load_earth_image(), you access the three global constants directly, just like you would do with any global variable in Python. However, you don’t change the value of any of the constants inside the function.
🌐
GeeksforGeeks
geeksforgeeks.org › python › global-local-variables-python
Global and Local Variables in Python - GeeksforGeeks
Declaring s as global tells Python to use the variable from the global scope. The function first appends " GFG", then reassigns s. Changes persist outside the function.
Published   September 20, 2025
🌐
W3Resource
w3resource.com › python-interview › how-do-you-access-global-variables-inside-a-function.php
Accessing and modifying global variables in Python functions
August 12, 2023 - Example: Access and modify a global variable inside a function · global_var = 100 # Declare global variable def access_global_variable_func(): # Accessing the said global variable print("Global variable value:", global_var) def modify_global_variable_func(): # Indicate that we are using the global variable global global_var # Update the global variable global_var += 50 access_global_variable_func() modify_global_variable_func() print("Value of the updated global variable:", global_var)
🌐
Python.org
discuss.python.org › python help
How to handle 'global' variables? - Python Help - Discussions on Python.org
May 8, 2024 - Hi, Newby question here. I’m creating a program that traverses a directory tree using os.walk. Each directory in that tree is checked for a distinct text file, the text file is opened, and the contents searched for image filenames & urls. I want to keep track of the total number of text files ...
Find elsewhere
🌐
Quora
quora.com › How-do-you-change-the-value-of-a-global-variable-inside-of-a-function-in-Python
How to change the value of a global variable inside of a function in Python - Quora
Answer (1 of 3): A global variable is one that has no hidden scope — its scope is everywhere. That means universal visibility. Globals are single points of dependency between all areas of your program. That is not a good idea. The mechanism (statement) to change a global is the same as for any v...
🌐
Great Learning
mygreatlearning.com › blog › it/software development › global variables in python
Global Variables in Python
August 15, 2024 - It's generally recommended to minimize ... values. ... To declare a global variable in Python, you need to use the global keyword followed by the variable name....
🌐
Python.org
discuss.python.org › python help
Can't use global variables within a function - Python Help - Discussions on Python.org
November 19, 2021 - hi again guys. Today i’m trying out functions, but i’ve run in a small (inconsistancy?) issue. Mainly i’m trying to print out global variables within a function, which works, but when I try to change the value of the …
🌐
Python
docs.python.org › 3 › faq › programming.html
Programming FAQ — Python 3.14.3 documentation
You could use a global variable containing a dictionary instead of the default value; it’s a matter of taste. Collect the arguments using the * and ** specifiers in the function’s parameter list; this gives you the positional arguments as a tuple and the keyword arguments as a dictionary.
🌐
Python.org
discuss.python.org › python help
Modifying variable outside of function - Python Help - Discussions on Python.org
December 2, 2024 - Think its a beginner question, but I do not understand why # some lines of working code minval = 10 maxval =22 test = 0 # a new added variable def a_function(): # some lines of working code test +=1 # new code line # some lines of working code # more lines of working a_function() # more working code now the problem in the test +=1 line show Unresolved reference ‘test’ while I can use, at least reading, other variables in functions Where am I wrong Thanks for help Regards ...
🌐
Python Morsels
pythonmorsels.com › assigning-global-variables
Assigning to global variables - Python Morsels
January 28, 2021 - But Python does allow you to do this even though you often shouldn't. The trick to writing a global variable is to use the global statement: >>> def set_message(name): ... global message ... message = f"Hello {name}" ... When we call this new ...
🌐
Python.org
discuss.python.org › python help
How to make variables global by default, Python 3.11 - Python Help - Discussions on Python.org
June 10, 2024 - In Python 3.11 the variables declared in the main program are not global by default and I cannot use them in functions. So if I want to use them in a function I have to use the global prefix/keyword.
🌐
Python Forum
python-forum.io › thread-15034.html
change value of a global variable across all modules
hello I wrote a minimum working example showing what I am unable to achieve: I have a variable in one.py called foo, i would like to change it's value and then use the new value accross all modules one.py import two foo = 'a' def main(): two...
🌐
Simplilearn
simplilearn.com › home › resources › software development › python global variables | definition, scope and examples
Python Global Variables | Definition, Scope and Examples
September 9, 2025 - Python global variables explained! Learn how to declare, modify and use them effectively while understanding their scope, limitations and best practices.
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
🌐
Sololearn
sololearn.com › en › Discuss › 2321393 › how-can-i-change-a-global-variable-from-a-function-without-the-use-of-global-keyword-python
How can I change a global variable from a function without the use of global keyword ? (Python) | Sololearn: Learn to code for FREE!
I just found a solution for that, what happened is that I reassigned the variable seq: seq = list() # to clear the list - I use it regularly - inside the function and that caused the problem, the code thought that meant to create a new variable, but I used the clear method of the list instead : seq.clear() and it worked.
🌐
Quora
quora.com › Can-you-change-a-global-variable-in-a-function-in-Python
Can you change a global variable in a function in Python? - Quora
Answer (1 of 3): Before starting the answer , I would like to inform you that please don't confuse with the indentation that I'll be mentioning in the below codes. PYTHONPROGRAM: [code]#Global scope. a = 10 #variable which is declared and defined globally. def val_a(): #random function that w...
🌐
Medium
medium.com › @python-javascript-php-html-css › using-global-variables-in-python-functions-1719bdca0609
Global Variable Utilization in Python Functions
August 24, 2024 - How do I declare a global variable inside a function? You use the global keyword followed by the variable name. Can I access a global variable without using the global keyword? Yes, you can access it, but you cannot modify it without the global ...
🌐
Python.org
discuss.python.org › python help
Global variables shared across modules - Python Help - Discussions on Python.org
June 25, 2022 - Hello to all Pythonians here. I encountered a strange behavior about the global keyword and modules, which I cannot understand. Module test1: Variable a is created Module test2: Module test1 is imported, and function f is created, which modifies variable a through the global keyword Module ...