Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

python edit global variable in function

globalvar = "flower"

def editglobalvar():
	global globalvar # accesses the "globalvar" variable, so when we change it
    # it won't assign the new value to a local variable,
    # not changing the value of the global variable
    
    globalvar = "good" # assigning new value
    
print(globalvar) # outputs "flower"
# if we didnt use the "global" keyword in the function, it would print out 
# "flower"
    
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #python #edit #global #variable #function
ADD COMMENT
Topic
Name
1+2 =