使用 self.xxxx 作为默认参数 - 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 输入一个默认参数,例如 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
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
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.
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)
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()
.
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.
EDIT
Here is all the code as requested:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
拉尔斯曼斯回答你的第一个问题
对于你的第二个问题,你能简单地三思而后行来避免递归吗?
larsmans answered your first question
For your second question, can you simply look before you leap to avoid recursion?
它不起作用,因为默认参数是在函数定义时计算的,而不是在调用时计算的:
常见策略是使用
None
默认参数。如果None
是有效值,则使用单例哨兵:It doesn't work because default arguments are evaluated at function definition time, not at call time:
The common strategy is to use a
None
default parameter. IfNone
is a valid value, use a singleton sentinel:如果您想将
None
视为有效参数,则可以使用**kwarg
参数。我还见过
...
或其他一些像这样使用的单例。If you want to treat
None
as a valid argument, you could use a**kwarg
parameter.I have also seen
...
or some other singleton be used like this.