Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Python, how do I check if a variable exists? [duplicate]

If "x" exists, then print "x exists".

I ask this because I always get this error:

UnboundLocalError at /settings/
local variable 'avatarlink' referenced before assignment
like image 446
TIMEX Avatar asked Dec 09 '22 12:12

TIMEX


1 Answers

Why do you need to know? If the code breaks because of this, it's probably because the code is wrong anyway and needs to be fixed.

That said, try checking if 'x' in locals() or if 'x' in globals(), as appropriate to where you're expecting it to be.

like image 124
Karl Knechtel Avatar answered Dec 24 '22 10:12

Karl Knechtel