Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python:重载运算符>;不同类型_Python_Python 3.x_Class_Operator Overloading_Operators - Fatal编程技术网

python:重载运算符>;不同类型

python:重载运算符>;不同类型,python,python-3.x,class,operator-overloading,operators,Python,Python 3.x,Class,Operator Overloading,Operators,告诉我如何重载操作符>(lt)或(gt)等,以便它可以处理不同的类型(尤其是0) 例如: class CRational(object): def __init__(self, a = 0, b = 1): self.a = a self.b = b def __neg__(self): return CRational(-self.a, self.b) def __add__(self, other)

告诉我如何重载操作符>(lt)或(gt)等,以便它可以处理不同的类型(尤其是0)

例如:

class CRational(object):

    def __init__(self, a = 0, b = 1):
        self.a = a
        self.b = b
        
    def __neg__(self):
        return CRational(-self.a, self.b)

    def __add__(self, other):
        return CRational(self.a * other.b + other.a * self.b, self.b * other.b)

    def __sub__(self, other):
        return CRational(self.a * other.b - other.a * self.b, self.b * other.b)

    def __mul__(self, other):
        return CRational(self.a * other.a, self.b * other.b)

    def __truediv__(self, other):
        return CRational(self.a * other.b, self.b * other.a)

    def __eq__(self, other):
        return self.a * other.b == other.a * self.b

    def __ne__(self, other):
        return not self.__eq__(other)

    def __lt__(self, other):
        return self.a * other.b < other.a * self.b

    def __le__(self, other):
        return self.a * other.b <= other.a * self.b

    def __gt__(self, other):
        return self.a * other.b > other.a * self.b

    def __ge__(self, other):
        return self.a * other.b >= other.a * self.b
你怎么修理它

s_a = "" if a > CRational(0) else "-"

上面描述的方法很有帮助,但并不漂亮:)

关于错误输出:您得到错误是因为您的“other”是一个int和
字符。\ugt\uu
正在尝试访问一个它没有
b
的属性:

def __gt__(self, other):  # passing in an int here
        return self.a * other.b > other.a * self.b

关于代码示例,假设
a
是另一个
CRational
对象,然后
a>0
将导致上面看到的
AttributeError
,唯一的修复方法是将其与另一个
CRational
对象或具有属性
a
b
的其他对象进行比较,听起来您想测试您正在使用的对象的类型。可能是这样的:

导入编号
定义(自身、其他):
如果isinstance(其他,键入(自身)):
返回self.a*other.b>其他.a*self.b
elif isinstance(其他,数字。数字):
返回self.a>其他
其他:
err\u str=“与%s对象比较的未知类型:%s”%(类型(自身)。\u名称\u,类型(其他)。\u名称\u)
上升(类型错误(错误)
这里,type(self)是获取CRational类的一种通用方法。这样,如果以后只需更改名称,就不必修改类的代码

isinstance检查给定对象是给定类型还是子类型


我编了一个数字的大小写,因为我不知道你想如何定义它。

如果你想比较一个
CRational
对象和一个int,那么你的
\uugt\uugt
方法也应该适用于整数。也就是说,如果
other
是一个整数,那么显然不能执行类似
other.b
的操作。以下是一个可能的解决方案:

class CRational:
    def __init__(self, a = 0, b = 1):
        self.a = a
        self.b = b
    def __gt__(self, other):
        if isinstance(other, CRational):
            return self.a * other.b > other.a * self.b
        elif isinstance(other, int):
            # Compare self (a CRational object) with other (an int)
            # TODO
        else:
            raise NotImplemented()
现在您可以执行以下操作:

a = CRational()
if a > 3:
    ...

不过要小心!即使您正确地实现了所有方法,您仍然无法执行
3>a
。秩序很重要
3>a
将调用int类的
\uu gt\uu
方法。您只能执行
a>3
a<3
a>=3
等操作。

对于您的代码示例,我看不出
a
是什么,也没有任何其他上下文来说明它试图实现的目标。请将它们包括在内。在函数中测试
其他
参数的类型
a = CRational()
if a > 3:
    ...