Global variable remains the same
Question:
I am trying to change a global variable, but it still goes back to the original value. For example, look at the response I get from this code:
0
prevValue before 0
prevValue changed 644.1324516367487
1
prevValue before 0
prevValue changed 67.37908666424572
I would like the prevValue
in the second iteration to be 644
.
Here is my code:
prevValue = 0
def csvData():
with open('test.csv', 'w', newline='') as f:
thewriter = csv.writer(f)
thewriter.writerow(['prevValue'])
for x in range(2):
print(x)
parse_results(thewriter,prevValue)
def parse_results(thewriter,prevValue):
value = getValue()
thewriter.writerow([prevValue])
print('prevValue before', prevValue)
prevValue = value
print('prevValue changed', prevValue)
Answers:
In Python, if there is a variable in the global scope with the same name as a variable in a function, the variable in the function is a reference to another memory block if you bind that variable name in the function.
Example:
foo = 42
def spam():
foo = "spam"
print(foo) # spam
spam()
print(foo) # 42
But if you do not bind the variable to a value, if you read the value, you read the global value.
Example:
foo = 42
def print_global():
print(foo) # 42
spam()
So how to change the value of a global? You must use the global keyword:
Example:
foo = 42
def spam():
global foo
foo = "spam"
print(foo) # spam
spam()
print(foo) # spam
Be careful, it’s not because you haven’t bind the variable to a value yet that python will let you read a global variable:
Example:
foo = 42
def spam():
print(foo) # Error : foo is undefined
foo = "spam"
print(foo)
spam()
In that case, since you bind foo
to a value in the function, python will disable any access to the global variable in the whole function.
I am trying to change a global variable, but it still goes back to the original value. For example, look at the response I get from this code:
0
prevValue before 0
prevValue changed 644.1324516367487
1
prevValue before 0
prevValue changed 67.37908666424572
I would like the prevValue
in the second iteration to be 644
.
Here is my code:
prevValue = 0
def csvData():
with open('test.csv', 'w', newline='') as f:
thewriter = csv.writer(f)
thewriter.writerow(['prevValue'])
for x in range(2):
print(x)
parse_results(thewriter,prevValue)
def parse_results(thewriter,prevValue):
value = getValue()
thewriter.writerow([prevValue])
print('prevValue before', prevValue)
prevValue = value
print('prevValue changed', prevValue)
In Python, if there is a variable in the global scope with the same name as a variable in a function, the variable in the function is a reference to another memory block if you bind that variable name in the function.
Example:
foo = 42
def spam():
foo = "spam"
print(foo) # spam
spam()
print(foo) # 42
But if you do not bind the variable to a value, if you read the value, you read the global value.
Example:
foo = 42
def print_global():
print(foo) # 42
spam()
So how to change the value of a global? You must use the global keyword:
Example:
foo = 42
def spam():
global foo
foo = "spam"
print(foo) # spam
spam()
print(foo) # spam
Be careful, it’s not because you haven’t bind the variable to a value yet that python will let you read a global variable:
Example:
foo = 42
def spam():
print(foo) # Error : foo is undefined
foo = "spam"
print(foo)
spam()
In that case, since you bind foo
to a value in the function, python will disable any access to the global variable in the whole function.