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
TIMEX
  • 259,804
  • 351
  • 777
  • 1,080
  • 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 http://stackoverflow.com/questions/575196/in-python-why-can-a-function-modify-some-arguments-as-perceived-by-the-caller-b/575337#575337 – jfs Dec 04 '10 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? – S.Lott Dec 04 '10 at 12:39
  • [How do i check if a variable exists in python](http://stackoverflow.com/questions/843277/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? – Anderson Green May 31 '13 at 03:51

5 Answers5

8

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.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • 2
    Just to clarify, you need to quote the name (`'avatarlink' in locals()`). But the first paragraph is the real solution. +1 –  Dec 04 '10 at 10:18
  • @delnan Of course; added quotes to reflect that. – Karl Knechtel Dec 04 '10 at 10:19
  • 3
    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. – Jim Brissom Dec 04 '10 at 10:21
  • -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 Dec 04 '10 at 12:02
  • @martineau I'm familiar with EAFP, but that seems like a pretty extreme application of the principle :) – Karl Knechtel Dec 04 '10 at 12:05
  • +1: "the code is wrong anyway and needs to be fixed." – S.Lott Dec 04 '10 at 12:39
  • @S.Lott: The code has been fixed...but who/what are you up-voting and quoting -- some deleted comment? – martineau Dec 04 '10 at 18:05
  • @Karl Knechtel: Since this is something that obviously shouldn't be happening in the first place, I think using such an "extreme" application of EAFP is completely warranted. It's more *lines* of code, but does the exact same sequence of of lookups that Python normally uses for any name which is what is failing and causing the error. It would also rule-out the possibility that the `'key' in whatever` approach in the answer just looked in the wrong place. – martineau Dec 04 '10 at 18:22
  • @martineau: "who/what are you up-voting"? This isn't a sensible question. One upvotes an answer. What else *could* be upvoted? "some deleted comment"? This isn't a sensible part of the question. The quoted words are part of the answer. Please clarify this comment -- it's really hard to parse. – S.Lott Dec 06 '10 at 12:04
  • @S.Lott: It's possible to up-vote comments as well as answers. I asked the question because I didn't see the words you quoted in the answer probably due to the way it was word-breaking in my browser -- so I didn't know what code was being referred to. Sorry for the confusion and thank you for ultimately telling me what I wanted to know. – martineau Dec 06 '10 at 15:32
  • Why should the code be wrong - you are expecting a next page cursor from a remote api, which will not be there if there isnt a next page. It is expected that it shouldnt be there when there isnt a next page. Otherwise remote api would not be JSON compliant. – unity100 Oct 23 '20 at 04:35
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.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • `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. – Karl Knechtel Apr 12 '23 at 09:20
2
try:
  variable
except NameError:
  print "It doesn't Exist!"
else:
  print "It exists!"
Rakesh Adhikesavan
  • 11,966
  • 18
  • 51
  • 76
1

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

Mariy
  • 5,746
  • 4
  • 40
  • 57
1

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

Jim
  • 22,354
  • 6
  • 52
  • 80