在Python中使用eq修改类__

2024-06-26 18:04:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要添加一个eq方法,如果坐标指向平面中的同一点(即,具有相同的x和y坐标),并且不知道如何执行该操作,则返回True。你知道吗

我尝试了一些使用eq的代码,但仍然有错误,我不太清楚原因。你知道吗

class Coordinate(object):
        def __init__(self, x, y):
            self.x = x
            self.y = y
    def getX(self):
        # Getter method for a Coordinate object's x coordinate.
        # Getter methods are better practice than just
        # accessing an attribute directly
        return self.x
    def getY(self):
        # Getter method for a Coordinate object's y coordinate
        return self.y
    def __str__(self):
        return '<' + str(self.getX()) + ',' + str(self.getY()) + '>'
    def __eq__(Coordinate, otherPoint):
        if self.GetX() == otherPoint.getX()&& self.GetY() == otherPoint.getY()
            return True

x=5
y=5

如果两个坐标是相同的数字,则预期输出将返回true;如果x和y不是相同的数字,则预期输出将返回false。你知道吗


Tags: selftruecoordinateforreturnobjectdefmethod
3条回答

很多小错误。。。你知道吗

  • GetXgetX不同
  • if语句的末尾需要一个:
  • __init__子句的缩进错误
  • &&在python中不存在,它被称为and
  • __eq__函数并不总是返回,它需要一个else子句,或者直接返回布尔表达式
  • __eq__的第一个参数必须是self
class Coordinate(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def getX(self):
        # Getter method for a Coordinate object's x coordinate.
        # Getter methods are better practice than just
        # accessing an attribute directly
        return self.x
    def getY(self):
        # Getter method for a Coordinate object's y coordinate
        return self.y
    def __str__(self):
        return '<' + str(self.getX()) + ',' + str(self.getY()) + '>'
    def __eq__(self, otherPoint):
        return self.getX() == otherPoint.getX() and self.getY() == otherPoint.getY()
>>> class Coordinate:
...     def __init__(self, x, y):
...         self.x = x
...         self.y = y
...     def __eq__(self, other):
...         if not isinstance(other, Coordinate):
...             raise TypeError('You can compare a Coordinate with only another Coordinate')
...         return self.x == other.x and self.y == other.y
...
>>> Coordinate(1,2) == Coordinate(1,2)
True
>>> Coordinate(1,2) == Coordinate(1,3)
False
>>> Coordinate(1,2) == 'Hello'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 7, in __eq__
TypeError: You can compare a Coordinate with only another Coordinate
>>>

代码中的一些问题/修复

  • 实际上您不需要GetXGetY,您可以直接引用属性xy。你知道吗
  • &&在Python中不是有效语法,而是使用and

  • __eq__的第一个参数将是self

  • 如果您使用的是python3.6+,则可以使用f-strings格式化字符串,否则可以使用格式化字符串
  • 您可以选择引发TypeError,以确保other的类型为Coordinate

因此,更新后的代码

class Coordinate(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __str__(self):
        #Use a f-string to format your string representation
        return f'<{self.x},{self.x}>'

        #If python3.5 and below, you can use format string as below
        #return '<{},{}>'.format(self.x, self.y)

    def __eq__(self, other):
        #If other is not an instance of Coordinate, raise TypeError
        if not isinstance(other, Coordinate):
            raise TypeError('An instance of class Coordinate is expected')
        #Compare both coordinates and return True or False
        return self.x == other.x and self.y == other.y

然后你可以像下面这样使用这个类

c1 = Coordinate(5,5)
c2 = Coordinate(6,6)

print(c1)
#<5,5>
print(c2)
#<6,6>
print(c1 == c1)
#True
print(c1 == c2)
#False
print(c1 == 1)
#TypeError: An instance of class Coordinate is expected

相关问题 更多 >