简体   繁体   English

如何将在 init 中声明的变量作为默认参数传递给 python 中的 class 方法

[英]How to pass a variable declared inside init as default parameter to a class method in python

I'm trying to perform a recursive search operation on a Binary Search Tree by declaring a BST class. I have initialised the root value to none(as shown below).我正在尝试通过声明 BST class 对二叉搜索树执行递归搜索操作。我已将根值初始化为无(如下所示)。

 class BST:  

        def __init__(self):
            self._root = None
        
        def search_recursive(self, troot, key):
            if troot:
                if key == troot._element:
                    return True
                elif key < troot._element:
                    return self.search_recursive(troot._left, key)
                elif key > troot._element:
                    return self.search_recursive(troot._right, key)
            else:
                return False

What I want to do is, pass this root as default argument for the recursive search function. So when it is called for the first time, the troot value will be the root and it will get updated to whatever is passed in the later function calls.我想要做的是,将此根作为递归搜索 function 的默认参数传递。因此,当它第一次被调用时,根值将是根,并且它将更新为在以后的 function 调用中传递的任何值. Something like this像这样的东西

def search_recursive(self, troot = self._root, key)

I can create a driver function and pass root as a parameter (as shown below) and get things working, but is their a way we can do as I have stated above?我可以创建一个驱动程序 function 并将 root 作为参数传递(如下所示)并让一切正常工作,但他们是我们可以按照我上面所说的那样做的方式吗?

  def search_driver(self, key):
      self.search_recursive(self._root, key)

Writing a non-recursive function that makes the initial call to the recursive function is the standard approach.编写对递归 function 进行初始调用的非递归 function 是标准方法。

In this specific case, however, you can omit the troot argument entirely, because you already have access to it via self .但是,在这种特定情况下,您可以完全省略troot参数,因为您已经可以通过self访问它。

class BST:    
        def __init__(self):
            self._root = None
        
        def search_recursive(self, key):
            if self._root:
                if key == self._root._element:
                    return True
                elif (key < self._root._element) and (self._root._left is not None):
                    return self._root._left.search_recursive(key)
                elif (key > self._root._element) and (self._root._right is not None):
                    return self._root._right.search_recursive(key)
            else:
                return False

You can write this code a bit more elegantly but I wanted to mimic your structure as closely as possible.您可以更优雅地编写此代码,但我想尽可能地模仿您的结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从python类函数内部计算方法中声明的变量 - How to calculate variable declared in a method from inside python class function 如何继承python 3中类的__init__方法内部定义的变量 - How to inherit a variable defined inside __init__ method of a class in python 3 如何从具有两个继承级别的类中覆盖 python __init__ 方法中参数的默认值: - How to overwrite default value of parameter in python __init__ method from class with two levels of inheritance: 如何将参数传递给 class 内部的 class - Python - How to pass a parameter to a class inside of a class - Python 如何自动将变量传递给类__init__方法? - How to pass a variable to a class __init__ method automatically? 如何将方法名称作为参数传递给python类 - how to pass method name as a parameter in python class Python:如何根据__init __()的参数设置类变量? - Python: How to set a class variable based on a parameter to __init__()? 如何使用Python获取在类内部声明为局部变量的字典的值 - How to get the value of a dictionary declared inside a class as a local variable with Python 如何在 class 内的 python __init__ 中使用变量 - How to use variable in python __init__ inside the class Python:在导入类后打印在 __init__.py 中声明的变量 - Python: Printing a variable declared in __init__.py after importing a class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM