I am trying to place a recursive formula inside a class statement
class SomeNode:
def __init__(self, a):
leng = len(a)
half= leng/2
self.firstnode=a[0][0]
self.child1=SomeNode([a[i]for k in range(leng)])
self.child2=SomeNode([a[j] for j in range(leng)])
def recursfunc(self):
print self.firstnode
recursfunc(self.child1)
recursfunc(self.child2)
However I keep getting the error message NameError: global name 'recursfunc' is not defined
Objects can have other objects as attribute values. When an object of some class has an attribute value of that same class, it is a recursive object.
Yes the following work, thanks.
When any function is called from main(), the memory is allocated to it on the stack. A recursive function calls itself, the memory for the called function is allocated on top of memory allocated to calling function and different copy of local variables is created for each function call.
Recursion is the technique of making a function call itself. This technique provides a way to break complicated problems down into simple problems which are easier to solve.
You need to use self.recursfunc()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With