I have a global variable I called Y_VAL which is ini开发者_运维技巧tialized to a value of 2.
I then have a function, called f() (for brevity), which uses Y_VAL.
def f():
y = Y_VAL
Y_VAL += 2
However, when trying to run my code, python gives the error message:
UnboundLocalError: local variable 'Y_VAL' referenced before assignment
If I remove the last line Y_VAL += 2
it works fine.
Why does python think that Y_VAL is a local variable?
You're missing the line global Y_VAL
inside the function.
When Y_VAL
occurs on the right-hand-side of an assignment, it's no problem because the local scope is searched first, then the global scope is searched. However, on the left-hand-side, you can only assign to a global that way when you've explicitly declared global Y_VAL
.
From the docs:
It would be impossible to assign to a global variable without global, although free variables may refer to globals without being declared global.
This is just how Python works: Assignment always binds the left-hand side name in the closest surrounding name space. Inside of a function the closest namespace is the local namespace of the function.
In order to assign to a global variable, you have to declare it global
. But avoid global
by all means. Global variables are almost always bad design, and thus the use of the global
keyword is a strong hint, that you are making design mistakes.
I ran to the same issue as you and as many others like you, before realising it needs the global statement. I then decided to move everything to object orientation and have piece of mind. Call me insane but I just dont trust myself with the global statement and its not difficult to come against a problem of local scope that is a pain to debug.
So I would advice collect all your "global" variables put them in a class inside an init(self) and not only you wont have to worry about local scope but you will have your code much better organised. Its not a freak of luck that most programmer out there prefer OOP.
精彩评论