Strange bug in Python mode

edited March 2016 in Python Mode

Hello everyone,

I had a code working perfectly well until recently, but now the strangest error has appeared. It boils down to the following lines (in python mode):

        time = 1.0

        def draw():

            time += 1

Which brings the error: UnboundLocalError: local variable 'time' referenced before assignment. Does anyone have an idea about why this is happening?

I have to add that i am running this on a different computer, where processing and python mode were installed very recently (which probably explains why it still works on the other computer).




  • In processing language, you would put int before time, in python I believe after minimal research that is the same. Try putting int before time.

  • Thanks for your fast answer.

    int is actually already a python function, so adding it like that raises a different exception. The bug is very surprising, this code was working alright not that long ago. I am wondering if the python mode got upgraded recently, i'll dig around..

  • True that could be a case. Hope you find what you are looking for. how about def draw():? Could that be a case? It could have been updated so that way of saying draw might not work anymore.

    Hope it helps

  • edited October 2014 Answer ✓

    I've just installed Python Mode about a week ago and tried to run its bundled examples via CTRL+SHIFT+O.
    Guess what, most of those was plagued by this very strange UnboundLocalError! ~X(
    Being a Python newbie, I thought the install was simply buggy! How come the very examples didn't run?! 8-}

    After some research I've found out the mystery: Python simply can't assign to "global" variables before they're declared global for each function!

    time = 0
    def setup(): frameRate(1)
    def draw():  global time; time+=5; print time,

    Although there's no need to declare them global if we're just reading their content! :-c
    However, Python Mode's examples gotta get fixed ASAP! [-(

  • They did change something in the python mode! I ran those exact three lines on the computer with the old version, and they work fine. The bundled examples have not been updated to work with the new version, which explains why they didn't run either...

    In any case, thanks a lot for the solution, i now have to redefine 50 variables as global :'(...

  • Copy your code to wordpad, go to find and replace. Find whatever you need to change and replace it to whatever you need to change it to

  • Like for example.

    Find "time" Replace to "global time"

  • Can you describe it? I can't really seem to be able to open it.

  • Answer ✓

    Sure. Basically in the previous mode, the autoglobal was turned on (meaning that all variables defined outside of a function where considered global, and could hence be modified anywhere in the code).

    While now, as shown by GoToLoop, any variable has to be redefined as global in any function where they are to be modified (simple reading access does not require this though).

  • I don't know what else you can do, I'm so sorry that I couldn't help.

  • No worries, i managed to fix it (in a not so elegant way). Thanks a lot for your time!

  • Hey, in the end of the day, it matters if the job gets done.

Sign In or Register to comment.