I recently started facing 'Unbound local variable issue' and I have no idea how did manage to code in python so long without knowing how to avoid this issue.
r = requests.get('http://xyz.example.com/api/v2/something', verify=False, timeout=10)
if r.status_code == 200 :
res = r.json()['result']
if x== 'organization':
return res
Traceback (most recent call last):
File "test.py", line 74, in <module>
resolve_name('test','organization')
File "test.py", line 20, in resolve_name
return res
UnboundLocalError: local variable 'res' referenced before assignment
What is the best way to avoid the 'UnboundlocalError' in this case?
So it looks like status code was not 200 and so res was never initialized. Make sure you initialize res on top. It looks like x has been set to "organization" somewhere above.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.