0

In a function, I need to perform some logic that requires me to call a function inside a function. What I did with this, like:

def dfs(problem):
 stack.push(bache)
 search(root) 
 while stack.isEmpty() != 0:
 def search(vertex):
 closed.add(vertex)
 for index in sars:
 stack.push(index)
 return stack

In the function, dfs, I am using search(root), is this is the correct way to do it?

I am getting an error: local variable 'search' referenced before assignment

BenMorel
37.1k53 gold badges208 silver badges339 bronze badges
asked Jul 17, 2010 at 2:18
1
  • 1
    As posted below, you need the definition above the call, that's it. I'm curious though, what is preventing you from writing that function outside the scope of the caller? Commented Jul 17, 2010 at 2:33

6 Answers 6

2

There are many mysterious bug-looking aspects in your code. The wrong order of definition (assuming you do need the search function to be a nested one) and the syntax error from the empty while loop have already been observed, but there are more...:

def dfs(problem):
 stack.push(bache)
 search(root) 

what's bache, what's stack, what's root? If they're all global variables, then you're overusing globals -- and apparently nowhere ever using the argument problem (?!).

 while stack.isEmpty() != 0:

what's this weird-looking method isEmpty? IOW, what type is stack (clearly not a Python list, and that's weird enough, since they do make excellent LIFO stacks;-)...? And what's ever going to make it empty...?

 def search(vertex):
 closed.add(vertex)

...don't tell me: closed is yet another global? Presumably a set? (I remember from a few of your Qs back that you absolutely wanted to have a closed dict, not set, even though I suggested that as a possibility...

 for index in sars:

...and what's sars?!

 stack.push(index)
 return stack

what a weird "loop" -- one that executes exactly once, altering a global variable, and then immediately returns that global variable (?) without doing any of the other steps through the loop. Even if this is exactly what you mean (push the first item of sars, period) I don't recommend hiding it in a pseudo-loop -- it seriously looks like a mystery bug just waiting to happen;-).

answered Jul 17, 2010 at 3:19
Sign up to request clarification or add additional context in comments.

Comments

1

You need to de-indent your search function. The way you have it set up right now you are defining your search function as a part of the completion of your dfs call. Also, encapsulation in a class would help.

answered Jul 17, 2010 at 2:21

Comments

1

Thats the wrong order. Try this:

def dfs(problem):
 def search(vertex):
 closed.add(vertex)
 for index in sars:
 stack.push(index)
 return stack
 stack.push(bache)
 search(root) 
 while stack.isEmpty() != 0:
answered Jul 17, 2010 at 2:22

Comments

1

Either define search before you call it, or define it outside of dfs.

answered Jul 17, 2010 at 3:04

Comments

0
  1. you have to define the function before using it
  2. root doesn't seem to be available in your scope - make sure it's reachable
answered Jul 17, 2010 at 2:21

Comments

0

You don't have body to your for your while loop. That is probably causing problems parsing the code. I would also suggest putting the local function definition before it is used, so it is easier to follow.

answered Jul 17, 2010 at 2:23

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.