4

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
3
  • 2
    To reiterate: In general, don't use locals(), globals(), don't catch UnboundLocalError, fix your code instead. Please, read links on scopes, namespaces, naming, and binding in Python provided in the answer stackoverflow.com/questions/575196/… Commented Dec 4, 2010 at 12:08
  • 3
    -1: If you always get this error, you have design issues. You don't need to "test" for a variable, you need to fix your design. Why are you "always" getting this error? What design mistake are you making? Commented Dec 4, 2010 at 12:39
  • How do i check if a variable exists in python has a title that is almost identical to this one. Are the two questions somehow distinct? Commented May 31, 2013 at 3:51

5 Answers 5

9

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.

Sign up to request clarification or add additional context in comments.

11 Comments

Just to clarify, you need to quote the name ('avatarlink' in locals()). But the first paragraph is the real solution. +1
@delnan Of course; added quotes to reflect that.
While this is indeed the correct answer to your question, please also consider Karl's remark that your code is probably broken if you need to employ a check like this. You are currently treating the symptoms, not the cause, which is always a bad idea.
-1 You don't need the if. Just put the variable's name inside a try with and except NameError:. Beside being easier, you're checking for existence the way Python does, not restricting it to locals, 'globals`, etc.
@martineau I'm familiar with EAFP, but that seems like a pretty extreme application of the principle :)
|
7

As they say in Python, "it's better to ask forgiveness than permission". So, just try and access the variable, and catch the error if it doesn't exist.

try:
    x
    print "x exists"
except UnboundLocalError:
    print "x doesn't exist"

However, I would really like to know why you think you need to do this. Usually, you would always set the variable before checking its value.

1 Comment

UnboundLocalError is a subtype of NameError. It would only be raised if x was determined at compile time to be a local variable, but hasn't yet been assigned. This can only happen inside a function, and only if there is an assignment to the name elsewhere in the function and there is no corresponding global or nonlocal declaration. Otherwise, a generic NameError will result.
2
try:
  variable
except NameError:
  print "It doesn't Exist!"
else:
  print "It exists!"

Comments

1

You may check if x is in globals() or locals().

Comments

1

In python you really shouldn't be using variables which haven't been set. If need be you can set avatarLink to None.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.