Typerror:无法解决的类型

use*_*408 0 python class typeerror

我正在写一个温度等级,我正在尝试比较temp1> temp2等等.

这是应该处理比较的函数定义之一的代码的一部分.

 def __eq__(self, other):
    if self.__valid==True and other.__valid==True:
        if self.__scale==other.__scale:
            if self.__mag==other.__mag:
                return True
            else:
                return False
            #if self.__mag>other.__mag:
                #return True
            #else:
                #return False
            #if self.__mag>=other.__mag:
                #return True
            #else:
                #return False
        else:
            if self.__scale=="C":
                A=other.celsius()
                if self.__mag==A.__mag:
                    return True
                else:
                    return False

            if self.__scale=="F":
                B=other.fahrenheit()
                if self.__mag==B.__mag:
                    return True
                else:
                    return False



    else:
        return False
Run Code Online (Sandbox Code Playgroud)

但当我这样做时:

A=Temperature(37.0, "C")<br>
B=Temperature(30.0, "C")<br>
print(A>B)
Run Code Online (Sandbox Code Playgroud)

我收到一个错误:

Traceback (most recent call last):
  File "C:\Users\owner\Desktop\temperature.py", line 218, in <module>
    print(A>B)
TypeError: unorderable types: Temperature() > Temperature()
Run Code Online (Sandbox Code Playgroud)

我试图比较两者的数量,但这是一个持续的问题.

Eri*_*got 5

__eq__()只处理相等测试.你想定义__le__()("≤")和朋友.

您可以使用functools.total_ordering()它来自动定义其他不等式运算符.

  • __le()__是'小于或等于'所以用"<="表示."<"使用__lt()__方法('严格小于') (2认同)