所以我定义了一个函数:
def getDistnace(self, strings, parentD, nodeName, nodeDistance):
我用它来调用:
Node.getDistnace(newNode, strings, parentD, nodeName=None, nodeDistance=None)
Và
Node.getDistnace(node, strings=None, parentD=None, nodeName, nodeDistance)
它们都来自另外两个不同的函数。但我的问题是我收到一条错误消息,指出在关键字 arg 之后有一个非关键字 arg
.
有什么办法可以解决这个错误吗?第一个 Node.getDistnace
Sẽ dây
Và parentD
thêm vào getDistance
中,第二个 Node.getDistnace
Sẽ nodeName
Và nodeDistance
添加到函数中。
你所有的参数都是位置性的,你根本不需要使用关键字:
Node.getDistnace(newNode, strings, parentD, None, None)
Node.getDistnace(node, None, None, nodeName, nodeDistance)
我认为您混淆了局部变量(您传递给函数的内容)和函数的参数名称。它们恰好在您的代码中匹配,但不要求它们匹配。
以下代码与您的第一个示例具有相同的效果:
arg1, arg2, arg3 = newNode, strings, parentD
Node.getDistnace(arg1, arg2, arg3, None, None)
Nếu bạnThực ra想使用关键字参数,那很好,但它们后面不能跟位置参数。然后你可以改变顺序,python 仍然会匹配它们:
Node.getDistnace(node, nodeDistance=nodeDistance, strings=None, parentD=None, nodeName=nodeName)
这里我将 nodeDistance
移到了关键字参数的前面,但是 Python 仍然会将它匹配到 getDistnace
方法的最后一个参数。
Tôi là một lập trình viên xuất sắc, rất giỏi!