1

I would like to check, whether a variable has been assigned or not. Based on the answers in this question Referring to the null object in Python, I tried it with if outputVariable==None but this does not work and throws the error "UnboundLocalError: local variable 'outputVariable' referenced before assignment".

I have the following code:

def testMethod(assignVariable):
    if assignVariable == True:
        outputVariable = 5
        
    if outputVariable==None:
        outputVariable = 0
 
    return outputVariable


returnValue = testMethod(False)

Any idea how I can check before the return statement, if outputVariable has a value. If it does not, it should be given a value of 0

Update: Based on the accepted answer here How do I check if a variable exists?, I tried the following but it was also not successfull:

def testMethod(assignVariable):
    if assignVariable == True:
        outputVariable = 5
        
    if outputVariable in locals():
        pass
    else:
        outputVariable = 0       

    return outputVariable


returnValue = testMethod(False)
PeterBe
  • 700
  • 1
  • 17
  • 37
  • well, the error is referring to that if assignVariable is False, then outputVariable is never assigned. So outputVariable == None is checking towards something that doesn't exist. – ziarra Nov 17 '21 at 14:30
  • 1
    Does this answer your question? [How do I check if a variable exists?](https://stackoverflow.com/questions/843277/how-do-i-check-if-a-variable-exists) – Dharman Nov 17 '21 at 14:50
  • What error does the updated code give you? – Zaid Al Shattle Nov 17 '21 at 15:12
  • @ZaidAlShattle: Thanks for the comment. The error message is "UnboundLocalError: local variable 'outputVariable' referenced before assignment" – PeterBe Nov 17 '21 at 15:13
  • 1
    for that specific case, you need to use `if 'outputVariable' in locals()` with the 's, I also posted an answer that will hopefully help you as well – Zaid Al Shattle Nov 17 '21 at 15:15

3 Answers3

2

This question has already been anwered here.

If the variable has no value, it is non-existent because you cannot declare a variable without any sort of initialization.

DoBaum
  • 41
  • 3
1

Here is a simple way to help you:

def tryVar(var):
    try:
        val = var
    except NameError:
        return None
    return val

In your code, just use tryVar(--) wherever you want to get a variable, you can check if said variable exists by simply:

if tryVar(X) is None:
   >>>DO WHATEVER

Solution for python 3.8+ if you want to assign variable, and if it doesnt exist assign 0 to it in one line:

Z = x if tryVar(x) is None else x:=0

this assigns the variable if its not assigned, and returns its value, additionally if you don't want to do anything with the values you can also:

x if tryVar(x) is None else x:=0
Zaid Al Shattle
  • 1,454
  • 1
  • 12
  • 21
  • Thanks for the comment. What I acutally want is to check whether a variable exists. If it exists it should be returned, if not a value of 0 should be assigned to it and then it should be returned. – PeterBe Nov 17 '21 at 15:19
  • 1
    Thanks Zaid. Actually your comment from above helps me (while this answer does not really help me). I had to use the ' before the variable name – PeterBe Nov 17 '21 at 15:20
  • 1
    @PeterBe No worries, as long as your issue was resolved :) – Zaid Al Shattle Nov 17 '21 at 15:21
0

You can't check for a variable that is not defined. Even None is a well-defined value for an object – it's a good default value in many scenarios.

This is the idiomatic way to achieve what you want:

def testMethod(assignVariable):

    return assignVariable or 0

returnValue = testMethod(False)

You can avoid that intermediate variable as it is not adding any value except being a placeholder; python takes care of all these checks internally. There is a ternary operator also if you need one. Function is optional if it's for one-time use.

Nishant
  • 20,354
  • 18
  • 69
  • 101
  • Thanks Nishant for your answer. Actually I have multiple variables that I want to return in my real application. How could I use your suggested appraoch with mutiple variables? – PeterBe Nov 17 '21 at 15:12
  • @PeterBe, You can have default values for each and then use the `or` like I said above. Please update the question with real example if you want me to update answer accordingly. – Nishant Nov 17 '21 at 17:16
  • 1
    Thanks for your answer Nishant. I could solve my issue by using the ' before the variable name of the variables in my updated question. But thanks anyways for your suggestion. – PeterBe Nov 17 '21 at 17:43