Trouble with variable scope

What I’m trying to do:
I am trying to use the value returned in this function in another part of code but keep getting this error.
I have printed out the return value of the function in the function but when I try to print it outafter the function call it won’t print.I cant figure out how to ‘use’ the return value of the function.
I’m sure it’s a very basic python problem,but learning is hard!


  def question_index(self):
    question_row = self.my_list[self.index]
    indexes_1 = question_row['question_index']
    print(indexes_1)  # this prints the value I'm looking for
  self.question_index()
    print (indexes_1)

NameError: name ‘indexes_1’ is not defined
at Homeform.questionForm, line 77
called from Homeform.questionForm, line 183
called from Homeform.questionForm, line 167

Just add self to it.

self.indexes_1 = question_row['question_index']

indexes_1 is a local variable doesn’t exist outside the scope of that function.

So you have two possible issues here -

  1. check your indentation on your code - the second block is not part of your function so the variable is out of scope. Assuming you have pasted it correctly of course :slight_smile: then try this :

  def question_index(self):
    question_row = self.my_list[self.index]
    indexes_1 = question_row['question_index']
    print(indexes_1)  # this prints the value I'm looking for

    self.question_index()
    print (indexes_1)
  1. If it must be accessible throughout your form then, as @divyeshlakhotia suggests, you should make it a form class-wide property by prepending self to it.

If you use self.indexes_1 then make sure you set it to a value before you read it. I think it’s good practise to initialise properties in the init method. So something like this :

def __init__(self, **properties):
    self.init_components(**properties)

    self.indexes_1 = 0

This ensures any attempt to read the value before you set it doesn’t give you nonsense or an error.

Some info on variable scope in Python :
https://pythonbasics.org/scope/

1 Like

many thanks
The help is much appreciated

1 Like

Thanks for the response.
very gratefull!

I see that you already have an answer, but I also see that you start the question with “value returned”.

Your function doesn’t return any value. It assigns a value to a variable.
If you want to access that value outside of the function, you could make that variable a global variable, but no, don’t do that, it’s a bad idea. I only mentioned this possibility because it has something to do with the subject of the post.

More likely you want that function to return the value, just like you said in your question, and that’s how you do it:

def question_index(self):
    question_row = self.my_list[self.index]
    indexes_1 = question_row['question_index']
    return indexes_1

print(question_index())
1 Like

Well spotted - I misread that :slight_smile:

Note to self - read the question …

You answered to the subject, I answered to the question, together we completed the job!

1 Like

Thank you so much guys.
It is truly amazing to have such great guidance
Maybe one day I can return the favor

1 Like