So I have a file, main.py
. The contents are such:
var = 0
def func():
var = 1
def pr():
func()
print(var)
pr()
When run, this prints out 0
instead of my expected 1
. Why and how can I get the changes in func()
to stick?
So I have a file, main.py
. The contents are such:
var = 0
def func():
var = 1
def pr():
func()
print(var)
pr()
When run, this prints out 0
instead of my expected 1
. Why and how can I get the changes in func()
to stick?
You do it like this by refering to var as a global in func():
var = 0
def func():
global var
var = 1
def pr():
func()
print(var)
pr()