0

I have the following function definition:

def rcsplit(arr):
 if np.all(arr==0): return [] # if all zeros return
 global res
 arr = delrc(arr) # delete leading/trailing rows/cols with all zeros
 indr = np.where(np.all(arr==0,axis=1))[0]
 indc = np.where(np.all(arr==0,axis=0))[0]
 if not indr and not indc: # If no further split possible return
 res.append(arr)
 return
 arr=np.delete(arr,indr,axis=0) #delete empty rows in between non empty rows
 arr=np.delete(arr,indc,axis=1) #delete empty cols in between non empty cols
 arr=np.split(arr,indc,axis=1) # split on empty (all zeros) cols
 arr2=[]
 for i in arr:
 z=delrc(i) 
 arr2.extend(np.split(z,indr,axis=0)) # split on empty (all zeros) rows
 for i in arr2:
 rcsplit(np.array(i))

The problem is I receive the following error:

NameError: global name 'res' is not defined

But this exact code works on other consoles. Is it my Python 2.7?

asked Nov 30, 2014 at 20:27
4
  • 1
    when do you recieve that error - what is the rest of your code - is there a smaller but complete code segment thatgenerates that error. Commented Nov 30, 2014 at 20:30
  • Why are you trying to use (and declaring) a global variable inside a function? Commented Nov 30, 2014 at 20:34
  • Yes, I noticed that I simply had to define res = [] in order for it to work. Would there be a way I don't have to use a global variable in this recursion? Commented Nov 30, 2014 at 20:39
  • A way to not use a global variable would be to add a res function argument with a default value of None, then in the function check to see if that's its value and initialize it to [] if so. When the function calls itself recursively, pass the current value of res in the call (to override the default value of None). Commented Nov 30, 2014 at 20:47

1 Answer 1

1

It seems that the global variable res is not defined before the function, maybe it is defined in the other console.

answered Nov 30, 2014 at 20:30
Sign up to request clarification or add additional context in comments.

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.