0

I want to use a construct like this, where a function is defined inside of another and can alter a value defined in the outer function:

def function1():
    res = []
    def function2():
        global res
        if (possibleToAnswer):
            res.append(answer)
        else:
            function2()
    return res
print (("%s") % function1(para))

It doesn't seem to work. I keep getting unbound bug. Any idea about how to get it to work?

4
  • What is this trying to accomplish? Commented Apr 21, 2017 at 4:06
  • 1
    You don't want global. Also, your example isn't complete and verifiable because certain parts (e.g. possibleToAnswer) aren't defined in what you've posted. Commented Apr 21, 2017 at 4:07
  • Also—you don't define function1 as accepting any parameters, yet you call it the parameter para. Commented Apr 21, 2017 at 4:16
  • Is your question addressed? If so, you can help future users by marking the checkbox beside the correct answer. If not, what can be clarified? Commented May 8, 2017 at 1:33

1 Answer 1

2

Don't use global—it's not in the immediate scope of function2, but it's not global.

def function1():
    res = []
    def function2():
        if (possibleToAnswer):
            res.append(answer)
        else:
            function2()
    return res
print (("%s") % function1(para))
Sign up to request clarification or add additional context in comments.

2 Comments

so all i have to do is just using it without "global''?
It's impossible for us to answer that until you provide a Minimal, complete, and verifiable example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.