python global variable trouble
Posted
by
Guanidene
on Stack Overflow
See other posts from Stack Overflow
or by Guanidene
Published on 2011-01-13T05:47:53Z
Indexed on
2011/01/13
5:53 UTC
Read the original article
Hit count: 512
python
|global-variables
I am having troubles using global variables in python...
In my program, i have declared 2 global variables, global SYNC_DATA and global SYNC_TOTAL_SIZE
Now in one of my functions, I am able to use the global variable SYNC_DATA without declaring it as global again in the function; however , I am not able to use the other global variable SYNC_TOTAL_SIZE in the same way. I have to declare the latter as global in the function again to use it. I get this error if i use it without declaring as global in the function - "UnboundLocalError: local variable 'SYNC_TOTAL_SIZE' referenced before assignment"
Why is it so that sometimes I can access global variables without declaring them as global in functions and sometimes not? And why Is it that we have to again declare it as global in the function when it is already declared once in the beginning... Why doesn`t the function just check the variable in the global namespace if it does not find it in its namespace directly?
© Stack Overflow or respective owner