Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

recursion within a class

Tags:

python

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

like image 788
LostLin Avatar asked Jun 14 '11 20:06

LostLin


People also ask

Can a class be recursive?

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.

Can we use recursion in class in Python?

Yes the following work, thanks.

How recursion works internally in Java?

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.

What is recursion class?

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.


1 Answers

You need to use self.recursfunc()

like image 137
AJ. Avatar answered Oct 20 '22 13:10

AJ.