Python 未保存的对象的ID

Python 未保存的对象的ID,python,python-3.x,Python,Python 3.x,我上了以下课程: import math class Point: """Two-Dimensional Point(x, y)""" def __init__(self, x=0, y=0): # Initialize the Point instance self.x = x self.y = y def __iter__(self):

我上了以下课程:

  import math

    class Point:
        """Two-Dimensional Point(x, y)"""
        def __init__(self, x=0, y=0):
            # Initialize the Point instance
            self.x = x
            self.y = y

        def __iter__(self):
             yield self.x
             yield self.y

        def __add__(self, other):
            addedx = self.x + other.x
            addedy = self.y + other.y
            return Point(addedx, addedy)

        def __mul__(self, other):
            mulx = self.x * other
            muly = self.y * other
            return Point(mulx, muly)

        def __rmul__(self, other):
            mulx = self.x * other
            muly = self.y * other
            return Point(mulx, muly)

        @classmethod
        def from_tuple(cls, tup):
            x, y = tup
            return cls(x, y)

        def loc_from_tuple(self, tup):
            self.x, self.y = tup

        @property
        def magnitude(self):
            # """Return the magnitude of vector from (0,0) to self."""
            return math.sqrt(self.x ** 2 + self.y ** 2)

        def distance(self, self2):
             return math.sqrt((self2.x - self.x) ** 2 + (self2.y - self.y) ** 2)

        def __str__(self):
            return 'Point at ({}, {})'.format(self.x, self.y)

        def __repr__(self):
            return "Point(x={},y={})".format(self.x, self.y)
我不知道该如何解释,但我基本上希望能够保持一个点id,尽管有数学运算。例如:

    point1 = Point(2, 3)
    point2 = Point(4, 5)
    id1 = id(point1)
    point1 += point2
    print(point1)
        Point(x=6, y=8)
    print(id1 == id(point1))
        True
    print(point2)
        Point(x=4, y=5)

我的代码中没有出现这种情况有什么原因吗。我的身份证上写着假的

id基本上是内存地址。如果你创建了一个新的对象,它可能会有一个不同的ID。如果你想要一个可变的点对象因为某种原因考虑,相反,它可以进行更新。你可以通过修改你的
\uu add\uu
来实现这一点,只修改
self.x
self.y
而不是返回一个新的
对象,但我不确定你为什么希望id保持不变。你从这些方法返回新的
对象,这可能是件好事,为什么不想要这个?ID在那里,因为数学操作应该返回新的点对象,而不修改原始点。这就是为什么给point1分配了ID。我修改了我的函数,只返回self.x+other.x,self.y+other.y。然而,这仍然返回false。
返回self.x+other.x,self.y+other.y
返回一个int,因此显然它不会有相同的I。再说一遍,你为什么要这个?我真的不明白你贴的其他内容。你说“数学运算应该返回新的点对象,而不修改原始点。”这就是他们所做的。这就是为什么你没有相同的身份证。