Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.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中的_truediv__的运算符重载_Python_Python 2.7_Overloading_Operator Keyword - Fatal编程技术网

python中的_truediv__的运算符重载

python中的_truediv__的运算符重载,python,python-2.7,overloading,operator-keyword,Python,Python 2.7,Overloading,Operator Keyword,我试图在python中实现除法运算符的重载 class Fraction: def __init__(self,top,bottom): def gcd(m, n): while m % n != 0: old_m = m old_n = n m = old_n n = old_m % old_n

我试图在python中实现除法运算符的重载

class Fraction:
    def __init__(self,top,bottom):
        def gcd(m, n):
            while m % n != 0:
                old_m = m
                old_n = n
                m = old_n
                n = old_m % old_n
            return n
        common = gcd(top,bottom)
        self.num = top/common
        self.den = bottom/common
    def __str__ (self):
        return str(self.num) + "/" + str(self.den)
    def get_num(self):
        return self.num
    def get_den(self):
        return self.den
    def __add__(self, other_fraction):
        new_num = self.num * other_fraction.den + self.den * other_fraction.num
        new_den = self.den * other_fraction.den
        return Fraction(new_num, new_den)
    def __sub__(self, other_fraction):
        new_num = self.num * other_fraction.den - self.den * other_fraction.num
        new_den = self.den * other_fraction.den
        return Fraction(new_num, new_den)
    def __mul__ (self, other_fraction):
        new_num = self.num * other_fraction.num
        new_den = self.den * other_fraction.den
        return Fraction(new_num, new_den)
    def __truediv__(self, other_fraction):
        new_num = self.num * other_fraction.den
        new_den = self.den * other_fraction.num
        return Fraction(new_num, new_den)

    def __eq__(self, other):
        first_num = self.num * other.den    
        second_num = other.num * self.den
        return first_num == second_num

a = Fraction(10,20)
b = Fraction(30,20)
print a
print "numerator is",a.get_num()
print "denominator is",a.get_den()
print "equality is",(a==b)
print "sum is",(a+b)
print "difference is",(a-b)
print "product is",(a*b)
print "division is",(a/b)
但是
\uuuu truediv\uuuuu
给出了如下错误

TypeError:/:“实例”和的操作数类型不受支持 “实例”


请告诉我代码有什么问题

对象。特殊方法仅与
/
运算符一起使用,并且仅当您已将Python编译器切换为与以下运算符一起使用真除法时:

from __future__ import division
如果未使用该导入,则
/
操作员将调用If present

另一方面,
/
操作符调用您未实现的。

来自:

除法运算符(/)由这些方法实现。这个
\uuuuu truediv\uuuu()方法在
\uuuu future\uuuu.division
生效时使用,否则使用
\uuuuu div()。如果这两种方法中只有一种是
定义后,该对象将不支持在替换中进行分割
上下文将改为引发TypeError

以及:

future语句是编译器的一个指令,它指定了一个特定的 [python程序]应使用以下语法或语义进行编译: 可在。。。Python的未来版本。未来 语句旨在简化到Python未来版本的迁移 这会给语言带来不兼容的变化。它允许使用 发布之前的新功能 功能成为标准

future_statement: from __future__ import feature
Python2.x识别的特性是unicode_文本, 打印函数、绝对导入、除法、生成器、嵌套作用域 和你的声明

现在,一些测试:

~$ python2.7
Python 2.7.6 (v2.7.6:3a1db0d2747e, Nov 10 2013, 00:42:54) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 3/2
1
>>> exit()

~$ python3.2
Python 3.2.3 (v3.2.3:3d0686d90f55, Apr 10 2012, 11:25:50) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 3/2
1.5
因此,在Python3.x中,
/
操作符的效果发生了变化。您也可以在下面的示例中看到:

class Dog(object):
    def __div__(self, other):
        print("__div__ called")
    def __truediv__(self, other):
        print("__truediv__ called")


Dog() / Dog()

--output:--
~/python_programs$ python2.7 myprog.py 
__div__ called

~/python_programs$ python3.4 myprog.py
__truediv__ called
由于Python2.x中的
/
操作符不调用
\uuuuuuuu truediv\uuuuu
,因此在Python2.x中重写
\uuuuu truediv\uuuu
无效

PEP 238 - PEP 238 -- Changing the Division Operator

We propose the following transitional measures:

    - Classic division will remain the default in the Python 2.x
      series; true division will be standard in Python 3.0.

    - The // operator will be available to request floor[, i.e. integer,] 
      division unambiguously.

    - The future division statement, spelled "from __future__ import
      division", will change the / operator to mean true division
      throughout the [program]
现在,看看这里发生了什么:

from __future__ import division

class Dog(object):
    def __div__(self, other):
        print("__div__ called")
    def __truediv__(self, other):
        print("__truediv__ called")


Dog() / Dog()

--output:--
~/python_programs$ python2.7 myprog.py 
__truediv__ called

~/python_programs$ python3.4 myprog.py
__truediv__ called
现在,您可以看到Python2.x中
/
操作符的python3.x效果。因此,现在您可以重写
\uuu truediv\uuu
,使
/
操作符执行您想要的操作


请注意,如果希望在Python3.x中使用整数除法,即
3/2=>1
,则必须使用
/
运算符,该运算符由
\uu floordiv\uu
实现。同样,如果在Python2.x中从uuu future uuu导入除法
,那么要得到整数除法,必须使用
/
操作符;如果你想重写类中的
/
操作符,你需要从分数导入分数实现
\\\\\\\\\\\\

,非常感谢你的详细回答
from __future__ import division

class Dog(object):
    def __div__(self, other):
        print("__div__ called")
    def __truediv__(self, other):
        print("__truediv__ called")


Dog() / Dog()

--output:--
~/python_programs$ python2.7 myprog.py 
__truediv__ called

~/python_programs$ python3.4 myprog.py
__truediv__ called