I have this method:
def is_active(self):
if self.is_current_node():
return True
cur_children = self.get_children()
while cur_children is not None:
for child in cur_children:
if child.is_current_node():
return True
raise Exception(child.display_name)
cur_children = cur_children.get_children()
return False
I put this method together and I put the raise Exception(child.display_name) to test out and “alert()” me to which child was being hit. The exception is never raised. You’d think that was because the function returned True on the if child.is_current_node() part. Well, if I replace the if part with this:
for child in cur_children:
if child.is_current_node():
raise Exception(child.display_name)
It doesn’t raise the exception still. If I do this however:
for child in cur_children:
raise Exception(child.display_name)
The exception is raised. I’m so confused. I’m sure this is something ridiculous but I was up till 2 and I can’t think straight enough to wrap my tiny brain around this.
Some ideas:
cur_children = self._children
while cur_children is not None:
for child in cur_children:
if child.is_current_node():
return True
raise Exception(child.display_name)
cur_children = cur_children._children
I assume that
self._childrencontains multiple children:[A, B, C]Then, on the first loop, it will take
A. Let’s assume thatAhas these children:[AA, AB, AC].Now, you do this:
cur_children = cur_children._children. This means that now, instead of continuing withBfrom the inital[A, B, C], it will continue withAA, and so on.In this example, it will never reach
B. Is this intended?What does your
is_current_node()contains? Probably you forgot to return a value, so the result is alwaysNone, andbool(None) == False.Another idea: (recursion)