如何在以下代码中将一个参数连接到另一个参数?

How can one parameter be connected to another parameter in the following code?

class Node:
  """
  A node class for A* pathfinding.
  :param parent:    parent of the current Node
  :param position:  current position of the Node in the maze
  :param g:         cost from start to current Node
  :param h:         heuristic based estimated cost for current Node to end Node
  :param f:         total cost of present node i.e., : f = g + h

  """

  def __init__(self, parent=None, position=None):
    self.parent = parent
    self.position = position

    self.g = 0
    self.h = 0
    self.f = 0

  def __eq__(self, other):
    return self.position == other.position

other参数如何与other.position中的position参数连接?

源代码:https://towardsdatascience.com/a-star-a-search-algorithm-eb495fb156bb

这段代码定义了一个对象类型NodeNode 对象的每个实例都有四个属性:positionfgh.

对象的 Node class 实例还具有与其他 Node 进行比较的能力(我们知道这一点是因为 __eq__() 已被定义为class).

Node class 的 __eq__() 方法将 Node class 的两个实例作为输入。这些在函数定义中称为 selfotherselfother都有上面提到的属于Node对象的四个属性:positionfgh.

这些属性的值通过 Python 的 'dot notation' 访问:例如,self.position 给出了调用该方法的对象的位置。 other.position 给出了您要比较的另一个 Node 的位置。

这里的关键是 other 指的是一个完全不同的对象,不是 self 的另一个属性。 (other 不一定是 Node;只要 other 也有一个名为 position 的属性,代码就可以工作;否则,它会引发一个 AttributeError.)

class Node:
  """
  A node class for A* pathfinding.
  :param parent:    parent of the current Node
  :param position:  current position of the Node in the maze
  :param g:         cost from start to current Node
  :param h:         heuristic based estimated cost for current Node to end Node
  :param f:         total cost of present node i.e., : f = g + h

  """

  def __init__(self, parent=None, position=None):
    self.parent = parent
    self.position = position
    self.g = 0
    self.h = 0
    self.f = 0

  # Compares the position of itself to a different Node's position
  def __eq__(self, other):
    return self.position == other.position