使用点符号调用函数与将其用作函数本身的参数之间的区别?
Difference between calling a function using dot notation vs. using it as an argument in the function itself?
我有一些执行基本深度优先搜索的代码:
class Node:
def __init__(self, name):
self.children = []
self.name = name
def addChild(self, name):
self.children.append(Node(name)) #recall I came across this before- 'Node(name)' is a node
return self
def depthFirstSearch(self, array): #self in this case is the root
array.append(self.name)
for child in self.children:
child.depthFirstSearch(array)
return array
我的问题是关于倒数第二行 child.depthFirstSearch(array)
,而不是这个,我们可以 depthFirstSearch(child,array)
吗?这些是否等效,因为后者是我通常调用函数的方式,而不是使用点符号。
我明白为什么它没有真正意义的一个原因,因为对于倒数第三行我们会有 child.children
,它不存在?
最后,一般什么时候用点号?还是不想用就不需要了
简单的答案是,是的,您可以删除 depthFirstSearch()
周围的点符号,但它会被认为比您现在拥有的略差:
class Node:
def __init__(self, name):
self.children = []
self.name = name
def addChild(self, name):
self.children.append(Node(name)) #recall I came across this before- 'Node(name)' is a node
return self
def depthFirstSearch(self, array): #self in this case is the root
array.append(self.name)
for child in self.children:
child.depthFirstSearch(array)
return array
# Free function version of depthFirstSearch()
def depthFirstSearch(node, array):
array.append(node.name)
for child in node.children:
depthFirstSearch(child, array)
return array
root = Node('root') # My guess at how you generate a root
... # Add more children to root and more children to those Nodes
array = root.depthFirstSearch([]) #My guess at how you call this now
array = depthFirstSearch(root, []) #You can call the free function like this
首选您的原始代码的原因是自由函数访问了它不需要知道的属性。有节点的名称:node.name
和节点的子节点:node.children
我有一些执行基本深度优先搜索的代码:
class Node:
def __init__(self, name):
self.children = []
self.name = name
def addChild(self, name):
self.children.append(Node(name)) #recall I came across this before- 'Node(name)' is a node
return self
def depthFirstSearch(self, array): #self in this case is the root
array.append(self.name)
for child in self.children:
child.depthFirstSearch(array)
return array
我的问题是关于倒数第二行 child.depthFirstSearch(array)
,而不是这个,我们可以 depthFirstSearch(child,array)
吗?这些是否等效,因为后者是我通常调用函数的方式,而不是使用点符号。
我明白为什么它没有真正意义的一个原因,因为对于倒数第三行我们会有 child.children
,它不存在?
最后,一般什么时候用点号?还是不想用就不需要了
简单的答案是,是的,您可以删除 depthFirstSearch()
周围的点符号,但它会被认为比您现在拥有的略差:
class Node:
def __init__(self, name):
self.children = []
self.name = name
def addChild(self, name):
self.children.append(Node(name)) #recall I came across this before- 'Node(name)' is a node
return self
def depthFirstSearch(self, array): #self in this case is the root
array.append(self.name)
for child in self.children:
child.depthFirstSearch(array)
return array
# Free function version of depthFirstSearch()
def depthFirstSearch(node, array):
array.append(node.name)
for child in node.children:
depthFirstSearch(child, array)
return array
root = Node('root') # My guess at how you generate a root
... # Add more children to root and more children to those Nodes
array = root.depthFirstSearch([]) #My guess at how you call this now
array = depthFirstSearch(root, []) #You can call the free function like this
首选您的原始代码的原因是自由函数访问了它不需要知道的属性。有节点的名称:node.name
和节点的子节点:node.children