Newbie issue; local variable referenced before assignment

Hello everybody!

I have only been working with python for about 4 days now, so i know my skillset is “somewhat” lackluster, but i fail to grasp what im doing wrong here…

I have a raspbery pico running a version of something called circuitpython.

I have 2 files; main.py and myDisplay.py.

some pseudo-code: (big chunks of code is missing to try to keep the “pseudo”-code as short as possible, but i think my error should be here somewhere…)

(main.py)
from myDisplay import doButtons
from myDisplay import doDisplay

buttonInterval = 0.1
displayInterval = 2.5


while True:
    counter = counter + 1
    doButtons()
    
    if counter > (displayInterval / buttonInterval):
        doDisplay()
        counter = 0
        
    time.sleep(buttonInterval)
(myDisplay.py)
BackLight_Val = 0.5
......
def doButtons(): #while True:
    if button_a.read():                                   # if a button press is detected then...
        if BackLight_Val < 0.9:
            BackLight_Val = BackLight_Val + 0.1
            print("MoreBrightness:")
            print(BackLight_Val)
            
        else:
            print("MaxedBrightness:")
            
        display.set_backlight(BackLight_Val)
            
    if button_b.read():
        if BackLight_Val > 0.11:
            BackLight_Val = BackLight_Val - 0.1
            print("LessBrightness:")
            print(BackLight_Val)
        else:
            BackLight_Val = 0.1
            print("NoBrightness:")
        
        display.set_backlight(BackLight_Val)
 ......

The error im getting is:
Traceback (most recent call last):
File “”, line 15, in
File “myDisplay.py”, line 61, in doButtons
NameError: local variable referenced before assignment

The module error line number corresponds with the function call for the doButtons function
The doButtons line number corresponds with the first time i use the BackLight_val variable inside a function.

I have been working with C for the past deckade (Arduino), so this feels like a namespace issue, but my logic dictates that the variable name is already “global” (inside that particular “module” named myDisplay"), since its declared before the function.

Ive been trying to get around this issue with no success, the rest of my program does work fine, but it crashes when i try to use a button to modify the backlight intensity.

The rule is that if you assign to a name in a function, the name is assumed to be local to that function unless you say otherwise with global or nonlocal.

In myDisplay.py, you’re assigning to BackLight_Val at the module level.

Also, in that same module, you’re assigning to BackLight_Val in the function doButtons, but, in doing so, Python is assuming that that name is local to the function. It’s not the same variable as the one that exists at the module level. You have 2 variables called BackLight_Val, one in the function’s namespace and another in the module’s namespace.

The solution is to tell Python that the local one is the same as the global one by adding the line:

global BackLight_Val

in the function.

1 Like

Thank you so much for that brilliant explanation @MRAB ! :smiling_face_with_three_hearts:

The python namespace works quite differently from what im used to (C), so i really appreciate that you took the time to explain it.