Python - very confused about method and while loop
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_childr开发者_StackOverflowen:
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.
If the first child in the list .is_current_node(), then the exception will never be raised in your first snippet.
Everything you have for evidence is supporting the idea that either self.is_current_node() is always true, or the first scanned child .is_current_node(). Given the third code snippet, the latter seems to be the case.
EDIT: removing a misunderstanding (child != self) :/
Actually, I have to ask, what is this supposed to do? It looks vaguely like recursive tree-traversal, but it's not quite there. (the cur_children = cur_children.get_children() line in particular is a bit strange)
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._children
contains multiple children: [A, B, C]
Then, on the first loop, it will take A
. Let's assume that A
has these children: [AA, AB, AC
].
Now, you do this: cur_children = cur_children._children
. This means that now, instead of continuing with B
from the inital [A, B, C]
, it will continue with AA
, 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 always None
, and bool(None) == False
.
Another idea: (recursion)
def is_active(self):
def check_children(children):
for child in children:
if child.is_current_node():
return True
else:
if children._children:
return check_children(children._children)
return False
if self.is_current_node():
return True
return check_children(children)
Maybe is_current_node always returns True and is_current_tab always returns False? I think more context is needed to give you an answer.
The only thing I can think of is that is_current_node changes state.
精彩评论