使用self.xxxx作为默认参数-Python
我试图简化我的一个家庭作业问题,并使代码变得更好。我使用的是一个二叉搜索树。现在,我在使用self.xxxx作为默认参数-Python,python,object,recursion,tree,Python,Object,Recursion,Tree,我试图简化我的一个家庭作业问题,并使代码变得更好。我使用的是一个二叉搜索树。现在,我在树()中有一个函数,它查找所有元素并将它们放入列表中 tree = Tree() #insert a bunch of items into tree 然后我使用makeList()函数从树中获取所有节点并将它们放入列表中。 要调用makeList()函数,我需要执行tree.makeList(tree.root)。对我来说,这似乎有点重复。我已经用tree.调用了tree对象,因此tree.root只是浪费
树()中有一个函数,它查找所有元素并将它们放入列表中
tree = Tree()
#insert a bunch of items into tree
然后我使用makeList()函数从树中获取所有节点并将它们放入列表中。
要调用makeList()
函数,我需要执行tree.makeList(tree.root)
。对我来说,这似乎有点重复。我已经用tree.
调用了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
类节点(对象):
定义初始化(自身,数据):
self.data=数据
self.lChild=无
self.rChild=None
类树(对象):
定义初始化(自):
self.root=None
定义(自我):
当前=自根
定义为空(self):
如果self.root==无:
返回真值
其他:
返回错误
def插件(自身,项目):
newNode=节点(项)
当前=自根
parent=self.root
如果self.root==无:
self.root=newNode
其他:
当前时!=无:
父项=当前
如果项目<当前数据:
current=current.lChild
其他:
current=current.rChild
如果项目
它不起作用,因为默认参数是在函数定义时计算的,而不是在调用时:
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.
拉斯曼:你的第一个问题是什么
对于你的第二个问题,你能不能三思而后行,避免递归
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="!!!")
您希望模块级方法中的“self”是什么?这绝对没有意义。如果makeList2()是类的一个方法,那么请提供正确的代码,而不是没有上下文的代码段。makeList2()应该是makeList(),我编辑了它。这有什么意义?我试图通过为树的根使用默认参数而不是调用它来简化makeList()函数的使用。我同意@crh878的观点,这是有意义的。我自己也尝试过,也是为了创建一个二叉搜索树。别开玩笑了……您可以省略哨兵
。只需使用NOTHING=object()
。保证生成一个唯一的单例,您可以通过is
进行检查。我强烈建议不要让foo(1,无)
和foo(1)
做不同的事情。@Glenn:使用一个必须处理任意对象的单参数函数,有时它是有意义的。好的,我理解它为什么不起作用,以及第一个代码段如何不断地附加您添加的任何值,但我没有完全了解第二个代码段中发生的事情。@crh878:关于使用None
的要点是最重要的。第二部分构造一个对象NOTHING
,将其用作默认参数,并检查是否传入了确切的对象。如果您不隐藏任何内容(不要从模块中导出),则客户端代码无法传入<代码>is
按内存地址比较对象。