Python &引用;启用“;班级比较

Python &引用;启用“;班级比较,python,class,object,Python,Class,Object,我想在我的CSC硬件上得到一些帮助。它是关于类/对象的,它是一个定义圆的简单类,名称为类circle(object) 硬件的确切文本(我完成了硬件的前两部分,因此第三部分是对初始问题的扩展): “”“通过使用诸如,>=、r2或r2>r1之类的运算符启用圆对象的比较,扩展圆类 定义(自身、循环1半径、循环2半径) #依此类推uuu lt_uuu()、uu le_uuu()、uu ne_uu()等 def main(): A=圆(3,4,1.5) B=圆(1,2,5.0) C=圆(5,7,7) D=

我想在我的CSC硬件上得到一些帮助。它是关于类/对象的,它是一个定义圆的简单类,名称为类circle(object)

硬件的确切文本(我完成了硬件的前两部分,因此第三部分是对初始问题的扩展):

“”“通过使用诸如,>=、r2或r2>r1之类的运算符启用圆对象的比较,扩展圆类 定义(自身、循环1半径、循环2半径) #依此类推uuu lt_uuu()、uu le_uuu()、uu ne_uu()等 def main(): A=圆(3,4,1.5) B=圆(1,2,5.0) C=圆(5,7,7) D=圆(9,8,3) 打印AC,A=C main() #输出应为“真、假、真、假”
我们是否必须为要在类中使用的每个方法定义/属性?请提前感谢。

简短回答:是。您必须定义要支持的比较运算符,除非您从基类继承此行为。

定义或覆盖类的比较运算符

看起来您的思路是正确的,只是您只需要将第二个圆对象传递给比较对象。self指的是第一个圆对象。因此self.r将给出第一个圆的r。此外,您还需要从该方法返回True或False

def __gt__(self, circle2):
    return self.r > circle2.r
请注意,这只是比较圆的r。

您可以使用from functools,如果您提供
\uuu eq\uuu()
和其他方法,它将生成所有缺少的比较方法

给定一个类定义一个或多个 丰富的比较排序方法 其余的由类装饰器提供。 这简化了所涉及的工作 指定所有可能的富 比较操作:

该类必须定义一个
\uu lt\uuu()
\uuu le\uuu()
\uu gt\uuu()
\uu ge\uu()
。此外,该类还应提供
\uuuu eq\uuuu()
方法

比如说,

@total_ordering
class Student:
    def _is_valid_operand(self, other):
        return (hasattr(other, "lastname") and
                hasattr(other, "firstname"))
    def __eq__(self, other):
        if not self._is_valid_operand(other):
            return NotImplemented
        return ((self.lastname.lower(), self.firstname.lower()) ==
                (other.lastname.lower(), other.firstname.lower()))
    def __lt__(self, other):
        if not self._is_valid_operand(other):
            return NotImplemented
        return ((self.lastname.lower(), self.firstname.lower()) <
                (other.lastname.lower(), other.firstname.lower()))
@total\u排序
班级学生:
def_是有效的操作数(self,other):
返回(hasattr(其他,“姓氏”)和
hasattr(其他“名字”))
定义(自身、其他):
如果不是self.\u是有效的\u操作数(其他):
返回未执行
返回((self.lastname.lower(),self.firstname.lower())==
(other.lastname.lower(),other.firstname.lower())
定义(自身、其他):
如果不是self.\u是有效的\u操作数(其他):
返回未执行
返回((self.lastname.lower(),self.firstname.lower())<
(other.lastname.lower(),other.firstname.lower())

这实际上并不是对您的问题本身的回答,但请注意,您的
Calcrea(…)
方法实际上不需要在类中。(请注意,没有理由在其中使用
self

事实上,您可能想要的是一个仍然在类中的方法

def area(self):
   return math.pi*(self.r**2)

所以它实际上使用了你所在圆的半径“

是的,我刚才也注意到了,谢谢你。我倾向于编写过多的程序,并留下一些通常不必要的代码,或者这些代码被认为是死板的。。。作为初学者,我想人们更倾向于关注程序的运行,而不是可读性和简洁性
import math
class Circle(object):
    def __init__(self, x=0, y=0, r=0):
        self.x = x
        self.y = y
        self.r = r
    def __str__(self):
        return "Circle at (%d , %d). Radius: %f" % (self.x, self.y, self.r)
    def calcArea(self, r):
        self.r = r
        return (math.pi)*(r**2)
    def __gt__(self, circ1Radius, circ2Radius)
        self.circ1Radius = circ1Radius
        self.circ2Radius = circ2Radius
        r1 = circ1Radius
        r2 = circ2Radius
        r1 > r2 or r2 > r1
    def __ge__(self, circ1Radius, circ2Radius)
    #And so on for __lt__(), __le__(), __ne__(), etc
def main():
    A = Circle(3,4,1.5) 
    B = Circle(1,2,5.0)
    C = Circle(5,7,7) 
    D = Circle(9,8,3)
    print A < B, B > C, A < C, A >= C
main()
#Output should be "True, False, True, False"
def __gt__(self, circle2):
    return self.r > circle2.r
@total_ordering
class Student:
    def _is_valid_operand(self, other):
        return (hasattr(other, "lastname") and
                hasattr(other, "firstname"))
    def __eq__(self, other):
        if not self._is_valid_operand(other):
            return NotImplemented
        return ((self.lastname.lower(), self.firstname.lower()) ==
                (other.lastname.lower(), other.firstname.lower()))
    def __lt__(self, other):
        if not self._is_valid_operand(other):
            return NotImplemented
        return ((self.lastname.lower(), self.firstname.lower()) <
                (other.lastname.lower(), other.firstname.lower()))
def area(self):
   return math.pi*(self.r**2)