使用 self.xxxx 作为默认参数 - Python [英] Using self.xxxx as a default parameter - Python

查看:66
本文介绍了使用 self.xxxx 作为默认参数 - Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试简化我的一个家庭作业问题并使代码更好一点.我正在使用的是二叉搜索树.现在我的 Tree() 类中有一个函数可以查找所有元素并将它们放入一个列表中.

I'm trying to simplify one of my homework problems and make the code a little better. What I'm working with is a binary search tree. Right now I have a function in my Tree() class that finds all the elements and puts them into a list.

tree = Tree()
#insert a bunch of items into tree

然后我使用我的 makeList() 函数从树中获取所有节点并将它们放在一个列表中.要调用 makeList() 函数,我执行 tree.makeList(tree.root).对我来说,这似乎有点重复.我已经用 tree 调用了树对象.所以 tree.root 只是浪费一点点输入.

then I use my makeList() function to take all the nodes from the tree and puts them in a list. To call the makeList() function, I do tree.makeList(tree.root). To me this seems a little repetitive. I'm already calling the tree object with tree.so the tree.root is just a waste of a little typing.

现在 makeList 函数是:

Right now the makeList function is:

    def makeList(self, aNode):
        if aNode is None:
            return []
        return [aNode.data] + self.makeList(aNode.lChild) + self.makeList(aNode.rChild)

我想让 aNode 输入一个默认参数,例如 aNode = self.root(它不起作用)这样我就可以用这个来运行函数,tree.makeList().

I would like to make the aNode input a default parameter such as aNode = self.root (which does not work) that way I could run the function with this, tree.makeList().

第一个问题是,为什么这不起作用?
第二个问题是,有没有办法让它起作用?正如你所看到的,makeList() 函数是递归的,所以我不能在函数的开头定义任何东西,否则我会陷入无限循环.

First question is, why doesn't that work?
Second question is, is there a way that it can work? As you can see the makeList() function is recursive so I cannot define anything at the beginning of the function or I get an infinite loop.

编辑这是要求的所有代码:

class Node(object):
    def __init__(self, data):
        self.data = data
        self.lChild = None
        self.rChild = None

class Tree(object):
    def __init__(self):
        self.root = None

    def __str__(self):
        current = self.root

    def isEmpty(self):
        if self.root == None:
            return True
        else:
            return False

    def insert (self, item):
        newNode = Node (item)
        current = self.root
        parent = self.root

        if self.root == None:
            self.root = newNode
        else:
            while current != None:
                parent = current
                if item < current.data:
                    current = current.lChild
                else:
                    current = current.rChild

            if item < parent.data:
                parent.lChild = newNode
            else:
                parent.rChild = newNode

    def inOrder(self, aNode):
        if aNode != None:
            self.inOrder(aNode.lChild)
            print aNode.data
            self.inOrder(aNode.rChild)

    def makeList(self, aNode):
        if aNode is None:
            return []
        return [aNode.data] + self.makeList(aNode.lChild) + self.makeList(aNode.rChild)


    def isSimilar(self, n, m):
        nList = self.makeList(n.root)
        mList = self.makeList(m.root) 
        print mList == nList 

推荐答案

larsmans 回答您的第一个问题

larsmans answered your first question

对于你的第二个问题,你能不能简单地在跳跃之前看一下以避免递归?

For your second question, can you simply look before you leap to avoid recursion?

def makeList(self, aNode=None):
    if aNode is None:
        aNode = self.root
    treeaslist = [aNode.data]
    if aNode.lChild:
        treeaslist.extend(self.makeList(aNode.lChild))
    if aNode.rChild:
        treeaslist.extend(self.makeList(aNode.rChild))
    return treeaslist

这篇关于使用 self.xxxx 作为默认参数 - Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆