yanivk1984
7/20/2019 - 3:22 PM

using global variable in python


# initial assignment of the variable
# can be skiped and declated it directly in the functions using the global keyword.
arg1 = 0

# changing the variable the first time
def func():
    # in order to use the global variable that is decaled in the outer scop, there is a need to use the global keyword
    global arg1
    arg1 = "changed the first time"

# changing the variable the seond time
def func2():
    # in order to use the global variable that is decaled in the outer scop, there is a need to use the global keyword
    global arg1
    arg1 = "changed the second time"
from global_exp import *
import global_exp

# call the first function that is going to change the variable
func()

# call to the seonc function that is going to change the variable
func2()

# print the final result
print(main2.arg1)