View Full Version : Recursive python method help
I can get this method to work without being in a class, but once its in a class it doesnt seem to work (note ive just started learning python):
class myClass():
def __init__(self):
self.children = []
def getChildren(self,obj):
self.children.append(obj.Name)
for each in obj.Children:
getChildren(each)
This however works(if i instantiate a list) :
test = []
def getChildren(arr, obj):
arr.append(obj.Name)
for each in obj.Children:
getChildren(each)
// null being a pointer to a null object
getChildren(test,null)
Any help would be appreiciated.
|
|
_stev_
05-24-2010, 11:42 PM
In your example, you need to call the object's "getChildren" method using the "self" parameter:
class myClass():
def __init__(self):
self.children = []
def getChildren(self, obj):
self.children.append(obj.Name)
for each in obj.Children:
self.getChildren(each)
Test it:
a = myClass()
a.getChildren( FBSystem().Scene.RootModel )
for name in a.children: print name
In a python class the first parameter self refers to the instance of the object you have created.
Stev
In your example, you need to call the object's "getChildren" method using the "self" parameter:
class myClass():
def __init__(self):
self.children = []
def getChildren(self, obj):
self.children.append(obj.Name)
for each in obj.Children:
self.getChildren(each)
Test it:
a = myClass()
a.getChildren( FBSystem().Scene.RootModel )
for name in a.children: print name
In a python class the first parameter self refers to the instance of the object you have created.
Stev
Awesome! yes I was just thinking about the very same thing walking back home . I like the 'rootModel' you have there, MB's ace!
Thanks again, (will definitely post in this forum with more questions)
Charles
CGTalk Moderation
05-24-2010, 11:56 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.
vBulletin v3.0.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.