'Access the function's variable above the function
I have a variable defined in a function, but I want to access the variable in a for loop above the function. I have globalized the variable but still not working. Is there a way to access a function's variable in above the function? Below is a sample code demonstration.
for i in range(1):
if a == 2:
do_something()
def add():
global a
b = 1
a = b + 1
return a
add()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|