Why does this code work:
var = 0
def func(num):
print num
var = 1
if num != 0:
func(num-1)
func(10)
but this one gives a "local variable 'var' referenced before assignment" error:
var = 0
def func(num):
print num
var = var
if num != 0:
func(num-1)
func(10)
-
See this answer and its comments for a discussion on why this is so.Lauritz V. Thaulow– Lauritz V. Thaulow2012年10月26日 18:26:33 +00:00Commented Oct 26, 2012 at 18:26
-
1possible duplicate of Short Description of Python Scoping RulesMartijn Pieters– Martijn Pieters2012年11月07日 18:21:58 +00:00Commented Nov 7, 2012 at 18:21
6 Answers 6
Because in the first code, you have created a local variable var and used its value, whereas in the 2nd code, you are using the local variable var, without defining it.
So, if you want to make your 2nd function work, you need to declare : -
global var
in the function before using var.
def func(num):
print num
var = 1 <-- # You create a local variable
if num != 0:
func(num-1)
Whereas in this code:
def func(num):
print num
var = var <--- # You are using the local variable on RHS without defining it
if num != 0:
func(num-1)
UPDATE: -
However, as per @Tim's comment, you should not use a global variable inside your functions. Rather deifine your variable before using it, to use it in local scope. Generally, you should try to limit the scope of your variables to local, and even in local namespace limit the scope of local variables, because that way your code will be easier to understand.
The more you increase the scope of your variables, the more are the chances of getting it used by the outside source, where it is not needed to be used.
10 Comments
If you have var = ... anywhere in a function, the name "var" will be treated as a local variable for the entire function, regardless of where that assignment occurs. This means that all occurrences of var in your function will be resolved in the local scope, so the right hand side of var = var results in the referenced before assignment error because var has not yet been initialized in the function's scope.
6 Comments
func would be a closure over var, and that the var = var line would have created a function-local variable, var, and assigned it the value of the module-level var. The "anywhere in a function" part of your answer smells magical to me. Surely that's not the actual cause?co_varnames attribute) at compilation time if they are used on the left-hand side of an assignment. This leads to (to me!) interesting situations like def foo(): var = var failing but def foo(): exec('var = var') being perfectly fine. I'd describe this as more of an implementation detail of an optimization pass than a feature of the language itself.You can read a global without declaring it global. But to write a global, you need to declare it global.
1 Comment
var without declaring it global in this case, because he's got a local name that overshadows it.In your second piece of code you have created a local variable in RHS and without defining it, you are assigning it to the LHS variable var which is global (a variable defined outside the function is considered global explicitly).
If your intention is to create a local variable inside the function and assign it to the value of the global variable, this will do the trick:
var = 0
def func(num):
print num
func.var = var # func.var is referring the local
# variable var inside the function func
if num != 0:
func(num-1)
func(10)
Comments
def runscan(self):
p = os.popen('LD_PRELOAD=/usr/libv4l/v4l1compat.so zbarcam
/dev/video0','r')
def input(self):
self.entryc.insert(END, code)
how about this? i want use local 'code' to the next function to insert the result of barcode to my Tkinter entryBox.. Thanks
1 Comment
Each function block is a local scope. If you want to assign to global variables, you need to do so explicitly:
var = 0
def func(num):
print num
global var
var = 1
if num != 0:
func(num-1)
print var # 0
func(2)
print var # 1
5 Comments
Explore related questions
See similar questions with these tags.