为了账号安全,请及时绑定邮箱和手机立即绑定

self.variable.function() 在 python 中的含义或通过类 self

self.variable.function() 在 python 中的含义或通过类 self

函数式编程 2022-08-16 16:32:47
我正在阅读二叉搜索树插入的简单方法,以下是解决方案:class Node:    def __init__(self, data):        self.left = None        self.right = None        self.data = data    def insert(self, data):        if self.data:            if data < self.data:                if self.left is None:                    self.left = Node(data)                else:                    self.left.insert(data)            elif data > self.data:                if self.right is None:                    self.right = Node(data)                else:                    self.right.insert(data)            else:                self.data = data    def PrintTree(self):        if self.left:            self.left.PrintTree()        print(self.data),        if self.right:            self.right.PrintTree()if __name__ == "__main__":    root = Node(12)    root.insert(6)    root.insert(14)    root.insert(3)    root.PrintTree()但我仍然想知道以下几行他们通过变量调用函数的地方:self.left.insert(data)然后:self.right.insert(data)和:self.left.PrintTree()这叫什么?我需要对此进行回顾。
查看完整描述

1 回答

?
慕运维8079593

TA贡献1876条经验 获得超5个赞

从该方法的内容中可以看出,和 instance 属性用于保存 类型的对象,这些对象是对节点的两个子节点的引用。您引用的行调用此类中定义的方法,但它们不是在实例上调用它们,而是在子节点上调用它们,因此它们将使用不同的数据。insert(self, data)rightleftNodeselfself



查看完整回答
反对 回复 2022-08-16
  • 1 回答
  • 0 关注
  • 60 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信