(2014-10-24, 01:26)Dilligaf Wrote: Get a script error when starting:
20:38:45 T:3176 ERROR: EXCEPTION Thrown (PythonToCppException) : -->Python callback/script returned the following error<--
- NOTE: IGNORING THIS CAN LEAD TO MEMORY LEAKS!
Error Type: <type 'exceptions.UnboundLocalError'>
Error Contents: local variable 'thread_street' referenced before assignment
Traceback (most recent call last):
File "C:\Users\mike\AppData\Roaming\XBMC\addons\weather.openweathermap.extended\default.py", line 960, in <module>
forecast(location, locationid, locationdeg)
File "C:\Users\mike\AppData\Roaming\XBMC\addons\weather.openweathermap.extended\default.py", line 306, in forecast
UnboundLocalError: local variable 'thread_street' referenced before assignment
-->End of Python script error report<--
cool, exactly the kind of bug i'm interested in.
fixed in: weather.openweathermap.extended-0.0.2.zip
This is because, even though exists, you're also using an assignment statement on the name inside of the function ( at the bottom line). Naturally, this creates a variable inside the function's scope called (truthfully, a or will only update (reassign) an existing variable, but for reasons unknown (likely consistency in this context), Python treats it as an assignment). The Python interpreter sees this at module load time and decides (correctly so) that the global scope's should not be used inside the local scope, which leads to a problem when you try to reference the variable before it is locally assigned.
Using global variables, outside of necessity, is usually frowned upon by Python developers, because it leads to confusing and problematic code. However, if you'd like to use them to accomplish what your code is implying, you can simply add:
inside the top of your function. This will tell Python that you don't intend to define a or variable inside the function's local scope. The Python interpreter sees this at module load time and decides (correctly so) to look up any references to the aforementioned variables in the global scope.
- the Python website has a great explanation for this common issue.
- Python 3 offers a related statement - check that out as well.