“自我”在做什么

2024-05-16 05:59:20 发布

您现在位置:Python中文网/ 问答频道 /正文

在这段代码中,他们正在检查self的相等性self.parent.leftChild. 他们为什么这么做

def isLeftChild(self):
    return self.parent and self.parent.leftChild == self

站点是:-class TreeNodehttp://interactivepython.org/runestone/static/pythonds/Trees/SearchTreeImplementation.html


Tags: and代码orgselfhttpreturn站点def
1条回答
网友
1楼 · 发布于 2024-05-16 05:59:20

Here in this code they are checking the equality of self with the other two parameters.

什么?哪里?不,a and b == ca和{}没有任何关联。在

self.parent and self.parent.leftChild == self

检查是否

  1. self.parent有一个有意义的值(不是None),如果是的话
  2. self.parent.leftChild等于给定的self。在

换句话说,它按照它的名字来做:它检查“我们”是否与我们父母的左子女相同。当然,这只有在我们有父母的情况下才有效。如果我们没有,我们就不是它的左撇子。在

注意:我刚才说的“不是None”只是一半的事实。确切地说,它检查是否存在“真值”,即在条件表达式上下文中计算为true的值。正如人们通常在缺少节点的情况下使用None,比如parent,我写的内容已经足够清楚了。在

相关问题 更多 >