Question

How to use a global variable in a function?

How do I create or use a global variable inside a function?

How do I use a global variable that was defined in one function inside other functions?


Failing to use the global keyword where appropriate often causes UnboundLocalError. The precise rules for this are explained at UnboundLocalError on local variable when reassigned after first use. Generally, please close other questions as a duplicate of that question when an explanation is sought, and this question when someone simply needs to know the global keyword.

 3985  4110243  3985
1 Jan 1970

Solution

 5221

You can use a global variable within other functions by declaring it as global within each function that assigns a value to it:

globvar = 0

def set_globvar_to_one():
    global globvar    # Needed to modify global copy of globvar
    globvar = 1

def print_globvar():
    print(globvar)     # No need for global declaration to read value of globvar

set_globvar_to_one()
print_globvar()       # Prints 1

Since it's unclear whether globvar = 1 is creating a local variable or changing a global variable, Python defaults to creating a local variable, and makes you explicitly choose the other behavior with the global keyword.

See other answers if you want to share a global variable across modules.

2009-01-08
Paul Stephenson

Solution

 915

If I'm understanding your situation correctly, what you're seeing is the result of how Python handles local (function) and global (module) namespaces.

Say you've got a module like this:

# sample.py
_my_global = 5

def func1():
    _my_global = 42

def func2():
    print _my_global

func1()
func2()

You might be expecting this to print 42, but instead, it prints 5. As has already been mentioned, if you add a 'global' declaration to func1(), then func2() will print 42.

def func1():
    global _my_global 
    _my_global = 42

What's going on here is that Python assumes that any name that is assigned to, anywhere within a function, is local to that function unless explicitly told otherwise. If it is only reading from a name, and the name doesn't exist locally, it will try to look up the name in any containing scopes (e.g. the module's global scope).

When you assign 42 to the name _my_global, therefore, Python creates a local variable that shadows the global variable of the same name. That local goes out of scope and is garbage-collected when func1() returns; meanwhile, func2() can never see anything other than the (unmodified) global name. Note that this namespace decision happens at compile time, not at runtime -- if you were to read the value of _my_global inside func1() before you assign to it, you'd get an UnboundLocalError, because Python has already decided that it must be a local variable but it has not had any value associated with it yet. But by using the 'global' statement, you tell Python that it should look elsewhere for the name instead of assigning to it locally.

(I believe that this behavior originated largely through optimization of local namespaces -- without this behavior, Python's VM would need to perform at least three name lookups each time a new name is assigned to inside a function (to ensure that the name didn't already exist at module/builtin level), which would significantly slow down a very common operation.)

2009-01-08
Jeff Shannon

Solution

 280

You may want to explore the notion of namespaces. In Python, the module is the natural place for global data:

Each module has its own private symbol table, which is used as the global symbol table by all functions defined in the module. Thus, the author of a module can use global variables in the module without worrying about accidental clashes with a user’s global variables. On the other hand, if you know what you are doing you can touch a module’s global variables with the same notation used to refer to its functions, modname.itemname.

A specific use of global-in-a-module is described here - How do I share global variables across modules?, and for completeness the contents are shared here:

The canonical way to share information across modules within a single program is to create a special configuration module (often called config or cfg). Just import the configuration module in all modules of your application; the module then becomes available as a global name. Because there is only one instance of each module, any changes made to the module object get reflected everywhere. For example:

File: config.py

x = 0   # Default value of the 'x' configuration setting

File: mod.py

import config
config.x = 1

File: main.py

import config
import mod
print config.x
2009-01-08
gimel

Solution

 123

Python uses a simple heuristic to decide which scope it should load a variable from, between local and global. If a variable name appears on the left hand side of an assignment, but is not declared global, it is assumed to be local. If it does not appear on the left hand side of an assignment, it is assumed to be global.

>>> import dis
>>> def foo():
...     global bar
...     baz = 5
...     print bar
...     print baz
...     print quux
... 
>>> dis.disassemble(foo.func_code)
  3           0 LOAD_CONST               1 (5)
              3 STORE_FAST               0 (baz)

  4           6 LOAD_GLOBAL              0 (bar)
              9 PRINT_ITEM          
             10 PRINT_NEWLINE       

  5          11 LOAD_FAST                0 (baz)
             14 PRINT_ITEM          
             15 PRINT_NEWLINE       

  6          16 LOAD_GLOBAL              1 (quux)
             19 PRINT_ITEM          
             20 PRINT_NEWLINE       
             21 LOAD_CONST               0 (None)
             24 RETURN_VALUE        
>>> 

See how baz, which appears on the left side of an assignment in foo(), is the only LOAD_FAST variable.

2011-07-12
SingleNegationElimination

Solution

 79

If you want to refer to a global variable in a function, you can use the global keyword to declare which variables are global. You don't have to use it in all cases (as someone here incorrectly claims) - if the name referenced in an expression cannot be found in local scope or scopes in the functions in which this function is defined, it is looked up among global variables.

However, if you assign to a new variable not declared as global in the function, it is implicitly declared as local, and it can overshadow any existing global variable with the same name.

Also, global variables are useful, contrary to some OOP zealots who claim otherwise - especially for smaller scripts, where OOP is overkill.

2009-01-08
J S

Solution

 72

If I create a global variable in one function, how can I use that variable in another function?

We can create a global with the following function:

def create_global_variable():
    global global_variable # must declare it to be a global first
    # modifications are thus reflected on the module's global scope
    global_variable = 'Foo' 

Writing a function does not actually run its code. So we call the create_global_variable function:

>>> create_global_variable()

Using globals without modification

You can just use it, so long as you don't expect to change which object it points to:

For example,

def use_global_variable():
    return global_variable + '!!!'

and now we can use the global variable:

>>> use_global_variable()
'Foo!!!'

Modification of the global variable from inside a function

To point the global variable at a different object, you are required to use the global keyword again:

def change_global_variable():
    global global_variable
    global_variable = 'Bar'

Note that after writing this function, the code actually changing it has still not run:

>>> use_global_variable()
'Foo!!!'

So after calling the function:

>>> change_global_variable()

we can see that the global variable has been changed. The global_variable name now points to 'Bar':

>>> use_global_variable()
'Bar!!!'

Note that "global" in Python is not truly global - it's only global to the module level. So it is only available to functions written in the modules in which it is global. Functions remember the module in which they are written, so when they are exported into other modules, they still look in the module in which they were created to find global variables.

Local variables with the same name

If you create a local variable with the same name, it will overshadow a global variable:

def use_local_with_same_name_as_global():
    # bad name for a local variable, though.
    global_variable = 'Baz' 
    return global_variable + '!!!'

>>> use_local_with_same_name_as_global()
'Baz!!!'

But using that misnamed local variable does not change the global variable:

>>> use_global_variable()
'Bar!!!'

Note that you should avoid using the local variables with the same names as globals unless you know precisely what you are doing and have a very good reason to do so. I have not yet encountered such a reason.

We get the same behavior in classes

A follow on comment asks:

what to do if I want to create a global variable inside a function inside a class and want to use that variable inside another function inside another class?

Here I demonstrate we get the same behavior in methods as we do in regular functions:

class Foo:
    def foo(self):
        global global_variable
        global_variable = 'Foo'

class Bar:
    def bar(self):
        return global_variable + '!!!'

Foo().foo()

And now:

>>> Bar().bar()
'Foo!!!'

But I would suggest instead of using global variables you use class attributes, to avoid cluttering the module namespace. Also note we don't use self arguments here - these could be class methods (handy if mutating the class attribute from the usual cls argument) or static methods (no self or cls).

2016-01-01
Aaron Hall

Solution

 61

In addition to already existing answers and to make this more confusing:

In Python, variables that are only referenced inside a function are implicitly global. If a variable is assigned a new value anywhere within the function’s body, it’s assumed to be a local. If a variable is ever assigned a new value inside the function, the variable is implicitly local, and you need to explicitly declare it as ‘global’.

Though a bit surprising at first, a moment’s consideration explains this. On one hand, requiring global for assigned variables provides a bar against unintended side-effects. On the other hand, if global was required for all global references, you’d be using global all the time. You’d have to declare as global every reference to a built-in function or to a component of an imported module. This clutter would defeat the usefulness of the global declaration for identifying side-effects.

Source: What are the rules for local and global variables in Python?.

2014-07-04
Rauni Lillemets

Solution

 44

With parallel execution, global variables can cause unexpected results if you don't understand what is happening. Here is an example of using a global variable within multiprocessing. We can clearly see that each process works with its own copy of the variable:

import multiprocessing
import os
import random
import sys
import time

def worker(new_value):
    old_value = get_value()
    set_value(random.randint(1, 99))
    print('pid=[{pid}] '
          'old_value=[{old_value:2}] '
          'new_value=[{new_value:2}] '
          'get_value=[{get_value:2}]'.format(
          pid=str(os.getpid()),
          old_value=old_value,
          new_value=new_value,
          get_value=get_value()))

def get_value():
    global global_variable
    return global_variable

def set_value(new_value):
    global global_variable
    global_variable = new_value

global_variable = -1

print('before set_value(), get_value() = [%s]' % get_value())
set_value(new_value=-2)
print('after  set_value(), get_value() = [%s]' % get_value())

processPool = multiprocessing.Pool(processes=5)
processPool.map(func=worker, iterable=range(15))

Output:

before set_value(), get_value() = [-1]
after  set_value(), get_value() = [-2]
pid=[53970] old_value=[-2] new_value=[ 0] get_value=[23]
pid=[53971] old_value=[-2] new_value=[ 1] get_value=[42]
pid=[53970] old_value=[23] new_value=[ 4] get_value=[50]
pid=[53970] old_value=[50] new_value=[ 6] get_value=[14]
pid=[53971] old_value=[42] new_value=[ 5] get_value=[31]
pid=[53972] old_value=[-2] new_value=[ 2] get_value=[44]
pid=[53973] old_value=[-2] new_value=[ 3] get_value=[94]
pid=[53970] old_value=[14] new_value=[ 7] get_value=[21]
pid=[53971] old_value=[31] new_value=[ 8] get_value=[34]
pid=[53972] old_value=[44] new_value=[ 9] get_value=[59]
pid=[53973] old_value=[94] new_value=[10] get_value=[87]
pid=[53970] old_value=[21] new_value=[11] get_value=[21]
pid=[53971] old_value=[34] new_value=[12] get_value=[82]
pid=[53972] old_value=[59] new_value=[13] get_value=[ 4]
pid=[53973] old_value=[87] new_value=[14] get_value=[70]
2013-10-03
Bohdan

Solution

 38

As it turns out the answer is always simple.

Here is a small sample module with a simple way to show it in a main definition:

def five(enterAnumber,sumation):
    global helper
    helper  = enterAnumber + sumation

def isTheNumber():
    return helper

Here is how to show it in a main definition:

import TestPy

def main():
    atest  = TestPy
    atest.five(5,8)
    print(atest.isTheNumber())

if __name__ == '__main__':
    main()

This simple code works just like that, and it will execute. I hope it helps.

2013-10-13
user2876408

Solution

 35

What you are saying is to use the method like this:

globvar = 5

def f():
    var = globvar
    print(var)

f()  # Prints 5

But the better way is to use the global variable like this:

globvar = 5
def f():
    global globvar
    print(globvar)
f()   #prints 5

Both give the same output.

2014-12-04
gxyd

Solution

 32

You need to reference the global variable in every function you want to use.

As follows:

var = "test"

def printGlobalText():
    global var #wWe are telling to explicitly use the global version
    var = "global from printGlobalText fun."
    print "var from printGlobalText: " + var

def printLocalText():
    #We are NOT telling to explicitly use the global version, so we are creating a local variable
    var = "local version from printLocalText fun"
    print "var from printLocalText: " + var

printGlobalText()
printLocalText()
"""
Output Result:
var from printGlobalText: global from printGlobalText fun.
var from printLocalText: local version from printLocalText
[Finished in 0.1s]
"""
2014-12-20
Mohamed El-Saka

Solution

 32

Try this:

def x1():
    global x
    x += 1
    print('x1: ', x)

def x2():
    global x
    x = x+1
    print('x2: ', x)

x = 5
print('x:  ', x)
x1()
x2()

# Output:
# x:   5
# x1:  6
# x2:  7
2015-02-04
Sagar Mehta

Solution

 29

You're not actually storing the global in a local variable, just creating a local reference to the same object that your original global reference refers to. Remember that pretty much everything in Python is a name referring to an object, and nothing gets copied in usual operation.

If you didn't have to explicitly specify when an identifier was to refer to a predefined global, then you'd presumably have to explicitly specify when an identifier is a new local variable instead (for example, with something like the 'var' command seen in JavaScript). Since local variables are more common than global variables in any serious and non-trivial system, Python's system makes more sense in most cases.

You could have a language which attempted to guess, using a global variable if it existed or creating a local variable if it didn't. However, that would be very error-prone. For example, importing another module could inadvertently introduce a global variable by that name, changing the behaviour of your program.

2009-01-09
Kylotan

Solution

 25

In case you have a local variable with the same name, you might want to use the globals() function.

globals()['your_global_var'] = 42
2017-04-07
Martin Thoma

Solution

 22

Following on and as an add on, use a file to contain all global variables all declared locally and then import as:

File initval.py:

Stocksin = 300
Prices = []

File getstocks.py:

import initval as iv

def getmystocks(): 
    iv.Stocksin = getstockcount()


def getmycharts():
    for ic in range(iv.Stocksin):
2015-10-24
user5473311

Solution

 18

Writing to explicit elements of a global array does not apparently need the global declaration, though writing to it "wholesale" does have that requirement:

import numpy as np

hostValue = 3.14159
hostArray = np.array([2., 3.])
hostMatrix = np.array([[1.0, 0.0],[ 0.0, 1.0]])

def func1():
    global hostValue    # mandatory, else local.
    hostValue = 2.0

def func2():
    global hostValue    # mandatory, else UnboundLocalError.
    hostValue += 1.0

def func3():
    global hostArray    # mandatory, else local.
    hostArray = np.array([14., 15.])

def func4():            # no need for globals
    hostArray[0] = 123.4

def func5():            # no need for globals
    hostArray[1] += 1.0

def func6():            # no need for globals
    hostMatrix[1][1] = 12.

def func7():            # no need for globals
    hostMatrix[0][0] += 0.33

func1()
print "After func1(), hostValue = ", hostValue
func2()
print "After func2(), hostValue = ", hostValue
func3()
print "After func3(), hostArray = ", hostArray
func4()
print "After func4(), hostArray = ", hostArray
func5()
print "After func5(), hostArray = ", hostArray
func6()
print "After func6(), hostMatrix = \n", hostMatrix
func7()
print "After func7(), hostMatrix = \n", hostMatrix
2016-01-07
Mike Lampton

Solution

 11
global_var = 10  # will be considered as a global variable


def func_1():
    global global_var  # access variable using variable keyword
    global_var += 1


def func_2():
    global global_var
    global_var *= 2
    print(f"func_2: {global_var}")


func_1()
func_2()
print("Global scope:", global_var) # will print 22

Explanation:

global_var is a global variable and all functions and classes can access that variable.

The func_1() accessed that global variable using the keyword global which points to the variable which is written in the global scope. If I didn't write the global keyword the variable global_var inside func_1 is considered a local variable that is only usable inside the function. Then inside func_1, I have incremented that global variable by 1.

The same happened in func_2().

After calling func_1 and func_2, you'll see the global_var is changed

2022-03-29
SHIVAM SINGH

Solution

 10

I'm adding this as I haven't seen it in any of the other answers and it might be useful for someone struggling with something similar. The globals() function returns a mutable global symbol dictionary where you can "magically" make data available for the rest of your code. For example:

from pickle import load
def loaditem(name):
    with open(r"C:\pickle\file\location"+"\{}.dat".format(name), "rb") as openfile:
        globals()[name] = load(openfile)
    return True

and

from pickle import dump
def dumpfile(name):
    with open(name+".dat", "wb") as outfile:
        dump(globals()[name], outfile)
    return True

Will just let you dump/load variables out of and into the global namespace. Super convenient, no muss, no fuss. Pretty sure it's Python 3 only.

2017-09-05
Rafaël Dera

Solution

 9

Reference the class namespace where you want the change to show up.

In this example, runner is using max from the file config. I want my test to change the value of max when runner is using it.

main/config.py

max = 15000

main/runner.py

from main import config
def check_threads():
    return max < thread_count 

tests/runner_test.py

from main import runner                # <----- 1. add file
from main.runner import check_threads
class RunnerTest(unittest):
   def test_threads(self):
       runner.max = 0                  # <----- 2. set global 
       check_threads()
2017-08-19
llewellyn falco

Solution

 9

Globals are fine - Except with Multiprocessing

Globals in connection with multiprocessing on different platforms/envrionments as Windows/Mac OS on the one side and Linux on the other are troublesome.

I will show you this with a simple example pointing out a problem which I run into some time ago.

If you want to understand, why things are different on Windows/MacOs and Linux you need to know that, the default mechanism to start a new process on ...

  • Windows/MacOs is 'spawn'
  • Linux is 'fork'

They are different in Memory allocation an initialisation ... (but I don't go into this here).

Let's have a look at the problem/example ...

import multiprocessing

counter = 0

def do(task_id):
    global counter
    counter +=1
    print(f'task {task_id}: counter = {counter}')

if __name__ == '__main__':

    pool = multiprocessing.Pool(processes=4)
    task_ids = list(range(4))
    pool.map(do, task_ids)

Windows

If you run this on Windows (And I suppose on MacOS too), you get the following output ...

task 0: counter = 1
task 1: counter = 2
task 2: counter = 3
task 3: counter = 4

Linux

If you run this on Linux, you get the following instead.

task 0: counter = 1
task 1: counter = 1
task 2: counter = 1
task 3: counter = 1
2020-05-24
thomas

Solution

 8

There are 2 ways to declare a variable as global:

1. assign variable inside functions and use global line

def declare_a_global_variable():
    global global_variable_1
    global_variable_1 = 1

# Note to use the function to global variables
declare_a_global_variable() 

2. assign variable outside functions:

global_variable_2 = 2

Now we can use these declared global variables in the other functions:

def declare_a_global_variable():
    global global_variable_1
    global_variable_1 = 1

# Note to use the function to global variables
declare_a_global_variable() 
global_variable_2 = 2

def print_variables():
    print(global_variable_1)
    print(global_variable_2)
print_variables() # prints 1 & 2

Note 1:

If you want to change a global variable inside another function like update_variables() you should use global line in that function before assigning the variable:

global_variable_1 = 1
global_variable_2 = 2

def update_variables():
    global global_variable_1
    global_variable_1 = 11
    global_variable_2 = 12 # will update just locally for this function

update_variables()
print(global_variable_1) # prints 11
print(global_variable_2) # prints 2

Note 2:

There is a exception for note 1 for list and dictionary variables while not using global line inside a function:

# declaring some global variables
variable = 'peter'
list_variable_1 = ['a','b']
list_variable_2 = ['c','d']

def update_global_variables():
    """without using global line"""
    variable = 'PETER' # won't update in global scope
    list_variable_1 = ['A','B'] # won't update in global scope
    list_variable_2[0] = 'C' # updated in global scope surprisingly this way
    list_variable_2[1] = 'D' # updated in global scope surprisingly this way

update_global_variables()

print('variable is: %s'%variable) # prints peter
print('list_variable_1 is: %s'%list_variable_1) # prints ['a', 'b']
print('list_variable_2 is: %s'%list_variable_2) # prints ['C', 'D']
2020-08-28
Mohsen Haddadi

Solution

 6

Though this has been answered, I am giving solution again as I prefer single line This is if you wish to create global variable within function

def someFunc():
    x=20
    globals()['y']=50
someFunc() # invoking function so that variable Y is created globally 
print(y) # output 50
print(x) #NameError: name 'x' is not defined as x was defined locally within function
2021-04-30
Pavn

Solution

 2

Like this code:

myVar = 12

def myFunc():
  myVar += 12

Key:

If you declare a variable outside the strings, it become global.

If you declare a variable inside the strings, it become local.

If you want to declare a global variable inside the strings, use the keyword global before the variable you want to declare:

myVar = 124
def myFunc():
  global myVar2
  myVar2 = 100
myFunc()
print(myVar2)

and then you have 100 in the document.

2022-02-11
Oscar Nguyen

Solution

 1
Initialized = 0  #Here This Initialized is global variable  

def Initialize():
     print("Initialized!")
     Initialized = 1  #This is local variable and assigning 1 to local variable
while Initialized == 0:  

Here we are comparing global variable Initialized that 0, so while loop condition got true

     Initialize()

Function will get called.Loop will be infinite

#if we do Initialized=1 then loop will terminate  

else:
    print("Lets do something else now!")
2022-04-15
zeeshan12396

Solution

 0

Maximal Syntax Sugar

I have a relatively simple Pythonic solution that transparently enables the syntactic sugar of global variable names as if they were in your current module, i.e. no v.my_var necessary, just my_var. This is important to me because I like my code to self-document, and our design document has formulas such as "HT3 < 39" meaning the hot water take temp is less than 39 degrees Celsius.

I've made it so that there is only slight burden placed on the coder, and have even include a test function (which you can delete) to prove that it works.

# main.py
from global_var import load_globals; load_globals(__name__, "variables")
from variables import *  # So that IDE removes squiggly underlines

# For test:
from modula_B import proof as proof1
from module_A import proof as proof2

H @ 103   # This is the new way of assigning, read "H at 101"
print("In main.py H =", H, id(H))

I = H + G
print("Result of H + G =", I)

# test:
proof1(H)
proof2(H)

# modula_B.py
# EXAMPLE OF IMPORTING GLOBALS: 🌎
from global_var import load_globals; load_globals(__name__, "variables")
from variables import *   # THIS IS FOR WINGWARE IDE TO REMOVE WARNING 
# UNDERLINES, BUT IS NOT REQUIRED.

def proof(h):
    assert h is H
    print("In module B, H = ", H, id(H))

# module_A.py
from global_var import load_globals; load_globals(__name__, "variables")
from variables import *

def proof(h):
    assert h is H
    print("In module A, H = ",H, id(H))

# variables.py - THIS IS WHERE YOU DECLARE YOUR PROJECT-WIDE GLOBALS 😎
from global_var import GlobalVar
    
H = GlobalVar(101)
G = GlobalVar(102)

---
# global_var.py - CONTAINS EXERCISES FOR THE READER TO COMPLETE
# BEFORE THEY FULLY UTILIZE THE STRATEGY.
from varname import varname   # OPTIONAL - gets the variable name so that you don't have
# to pass it into Ctor; was going to use it for this algorithm.  Left it because it's quite
# awesome for Python CAS libraries for instance, where you just want to do X = ZZ() and not
# X = ZZ("X") which seems intuitively dumb, but usually has to be done.

import sys
import importlib

class GlobalVar:
    def __init__(self, value, name=None):
        if name is None:
            name = varname()
        self._value = value
        
    def __matmul__(self, v):        # This is your new way of assigning to a global
        """@ operator - documentation says 'matrix multiplication' 😂
        """
        if isinstance(v, GlobalVar):
            v = v.value    # We are not chaining GlobalVars
        
        self._value = v
        
    def __add__(self, v):
        """Example to follow for each operator."""
        if isinstance(v, GlobalVar):
            v = v.value
        return self.value + v      # Yes, completely forget GlobalVar.
        
    @property
    def value(self):
        return self._value
    
    # TODO for the reader:
    # https://docs.python.org/3/library/operator.html
    # Implement overrides for each of those operators in this class;
    # except of course @.  If you needed to use @ for something
    # else, then replace @ with any one of those operators. I'm
    # fairly certain you're not using all 30 or so operators in your
    # code base!
    
    def __str__(self):
        return str(self._value)
    
    def __repr__(self):
        return repr(self._value)
    
    
def load_globals(module_name, var_module):
    importlib.import_module(var_module)       # sys.modules[...] 
     # Won't work unless we import first
    this_module =  sys.modules[var_module]
    module = sys.modules[module_name]
    
    for attrib in dir(this_module):
        value = getattr(this_module, attrib)
        
        if isinstance(value, GlobalVar):
            setattr(module, attrib, value)

I've only tested on my RaspberryPi4 because I'm coding on a solar heating system. I wanted the syntax sugar to match EXACTLY what's in their design document for obvious reasons.

Remember to pip install -U varname should you decide to keep that feature; read the code comments.

🌌🚀

2024-04-02
Debug

Solution

 0

Most of the answers here are great but some are very long, especially for those who seek short-yet-to-the-point answer.

So, in a unit/file each variable outside any function is considered a global variable. And if you'd like to use it inside a function of that unit you'll need to explicitly declate you're referring to that global variable.

Here is a simple example:

Say, I need a static/global variable to keep its value during the entire running of the program and not reset when leaving its scope, and say we need to increase it by 1 every time we enter the function, or set it to a new integer value.

A correct and simple way to do it is this:

myGlobalVar = 0

def foo(newVal=None):

    global myGlobalVar #declare you are reffering to the global var above

    if((newVal==None) or (not str(newVal).isnumeric())):
        myGlobalVar = myGlobalVar + 1
    else:
        myGlobalVar =  newVal

    print("myGlobalVar is: " + str(myGlobalVar))

If we call form anywhere in the code (other modules/files/functions):

lg.foo()
lg.foo()
lg.foo(13)
lg.foo()
lg.foo("asd12")
lg.foo()
lg.foo(0)
lg.foo()
lg.foo()

We'll get:

myGlobalVar is: 1
myGlobalVar is: 2
myGlobalVar is: 13
myGlobalVar is: 14
myGlobalVar is: 15
myGlobalVar is: 16
myGlobalVar is: 0
myGlobalVar is: 1
myGlobalVar is: 2
2024-07-11
JamesC

Solution

 -4

if you want to access global var you just add global keyword inside your function ex: global_var = 'yeah'

def someFunc():
   global global_var;
   print(nam_of_var)
2022-12-01
zaldi 1