Why Do I Get a "Referenced Before Assignment" Error When Assigning to a Global Variable in a Function

Why do I get a referenced before assignment error when assigning to a global variable in a function?

I think you are using 'global' incorrectly. See Python reference. You should declare variable without global and then inside the function when you want to access global variable you declare it global yourvar.

#!/usr/bin/python

total

def checkTotal():
global total
total = 0

See this example:

#!/usr/bin/env python

total = 0

def doA():
# not accessing global total
total = 10

def doB():
global total
total = total + 1

def checkTotal():
# global total - not required as global is required
# only for assignment - thanks for comment Greg
print total

def main():
doA()
doB()
checkTotal()

if __name__ == '__main__':
main()

Because doA() does not modify the global total the output is 1 not 11.

Global variable declaration + Unbound Error: local variable referenced before assignment?

Simply declare it global within the function instead.

def countPulse(channel): 
global rotation
rotation = rotation+1
...

Getting error when I try to print a global variable in a function in Python3

Python interprets this line: a="banana" in the function as the definition of a new, local, variable a. This variable in the scope of the function replaces the global variable a. Note that print(a) (reference to local variable a) occurs before a="banana" (= assignment). Hence you get the error: UnboundLocalError: local variable 'a' referenced before assignment.

SEE ALSO:

Why am I getting an UnboundLocalError when the variable has a value?

Python gotchas

The 10 Most Common Mistakes That Python Developers Make

Local variable referenced before assignment?

When Python parses the body of a function definition and encounters an assignment such as

feed = ...

Python interprets feed as a local variable by default. If you do not wish for it to be a local variable, you must put

global feed

in the function definition. The global statement does not have to be at the beginning of the function definition, but that is where it is usually placed. Wherever it is placed, the global declaration makes feed a global variable everywhere in the function.

Without the global statement, since feed is taken to be a local variable, when Python executes

feed = feed + 1,

Python evaluates the right-hand side first and tries to look up the value of feed. The first time through it finds feed is undefined. Hence the error.

The shortest way to patch up the code is to add global feed to the beginning of onLoadFinished. The nicer way is to use a class:

class Page(object):
def __init__(self):
self.feed = 0
def onLoadFinished(self, result):
...
self.feed += 1

The problem with having functions which mutate global variables is that it makes it harder to grok your code. Functions are no longer isolated units. Their interaction extends to everything that affects or is affected by the global variable. Thus it makes larger programs harder to understand.

By avoiding mutating globals, in the long run your code will be easier to understand, test and maintain.



Related Topics



Leave a reply



Submit