self.variable.function() 在 python 中的含义或通过类自变量访问方法

卢塔亚·胡扎伊法·伊德里斯

我正在阅读这种二叉搜索树插入的简单方法,以下是解决方案:

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()

这个叫什么 ?我需要回顾一下。

亚当·霍斯曼

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

self的含义。__dict__ =类定义中的self

__init __(self)-> None:vs __init __(self):在python中

模块为无属性-self.variable.function(self)

在类中使用没有self的变量-Python

python中的类方法:来自self属性的默认kwargs

从表WHERE COLUMN = self.variable中选择python python

从python中的另一个类访问self

self如何在python的类中工作

类方法中定义的python函数可以访问self吗?

Python:为什么不能在方法中使用self.variable作为可选参数?

在方法链接时,如何在python类方法中返回self和另一个变量?

Python类:覆盖“ self”

是否可以使用python中__init__的self.variable值初始化方法的参数?

如何从python类函数访问对self的调用

没有在python中使用类,但是我的函数需要“ self”自变量?

Python新手:使用eval()为self.variable赋值

Python类中的Lambda指的是self

python中self.variable名称和classname.variable之间的区别

python:函数内的self.variable

Self 不会传递给 Python 中的类方法

子类继承父类 self.variable

获取类中的 ipywidget 按钮以通过 self 访问类参数?

Python:在内部嵌套类中使用来自外部类的 self.variable

function_f()(variable_v) 的含义和工作(一个函数后跟 () 中的变量)

使用 self 变量作为 python 方法参数

python中self.object的含义是什么?

在python类之外使用类自变量创建方法,然后将其作为方法添加到类中

python递归函数使用self.function

在方法中访问类变量的方法 - python