使用self.xxxx作为默认参数

2024-09-30 01:21:23 发布

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

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

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

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

现在,makeList函数是:

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

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

第一个问题是,为什么这不起作用?
第二个问题是,有没有一种方法可以奏效?正如您所看到的,makeList()函数是递归的,因此我无法在函数的开头定义任何内容,或者我得到了一个无限循环

编辑 以下是所需的所有代码:

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 

Tags: 函数selfnonetreedatareturnifdef
3条回答

拉斯曼answered你的第一个问题

对于你的第二个问题,你能不能三思而后行,避免递归

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

如果要将None视为有效参数,可以使用**kwarg参数

def function(arg1, arg2, **kwargs):
    kwargs.setdefault('arg3', default)
    arg3 = kwargs['arg3']
    
    # Continue with function

function("amazing", "fantastic") # uses default
function("foo", "bar", arg3=None) # Not default, but None
function("hello", "world", arg3="!!!")

我还看到...或其他一些单例被这样使用

def function(arg1, arg2=...):
    if arg2 is ...:
        arg2 = default

它不起作用,因为默认参数是在函数定义时计算的,而不是在调用时:

def f(lst = []):
    lst.append(1)
    return lst

print(f()) # prints [1]
print(f()) # prints [1, 1]

常用的策略是使用None默认参数。如果None是有效值,请使用单例哨兵:

NOTHING = object()

def f(arg = NOTHING):
    if arg is NOTHING:
        # no argument
    # etc.

相关问题 更多 >

    热门问题