Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.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中通过重写_neg__实现否定_Python_Class - Fatal编程技术网

在python中通过重写_neg__实现否定

在python中通过重写_neg__实现否定,python,class,Python,Class,我现在正在学习课程,我试图制作一个关于分数的程序。 我发布了一些代码摘录: class Fraction(): def __init__(self,numerator,denominator=1): g = gcd(numerator,denominator) self.numerator = numerator / g self.denominator = denominator / g def __add__(self,oth

我现在正在学习课程,我试图制作一个关于分数的程序。 我发布了一些代码摘录:

class Fraction():
    def __init__(self,numerator,denominator=1):
        g = gcd(numerator,denominator)
        self.numerator = numerator / g
        self.denominator = denominator / g

    def __add__(self,other):
        if isinstance(other,int):
            other = Fraction(other)
        return Fraction((self.numerator * other.denominator + self.denominator * other.numerator),(self.denominator * other.denominator))

    def __neg__(self):
        return (-self.numerator , self.denominator)

    def __sub__(self,other):
        other = other.__neg__()
        return self + (-other)

    def __mul__(self , other):
        if isinstance(other,int):
            other = Fraction(other)
        return Fraction(self.numerator*other.numerator,self.denominator*other.denominator)

    __rmul__ = __mul__

    def printit(self):
        print '(' + str(self.numerator) + '/' + str(self.denominator) + ')'

def gcd(m,n):
    if(m%n == 0):
        return n
    else:
        return gcd(n,m%n)

#Input is of the form

    Fraction.printit(Fraction(5,6) + Fraction(1,6))
    Fraction.printit(Fraction(5,6) - Fraction(1,6))
先前的缩进错误已得到处理。当我将我的代码从这个页面复制到记事本++时,出现了某种故障。再次缩进解决了这个问题。 现在出现的错误是:

(1/1)     #This is the first answer, works perfectly. 

Traceback (most recent call last):
File "Fractions.py", line 36, in <module>
    Fraction.printit(Fraction(5,6) - Fraction(1,6))
File "Fractions.py", line 15, in __sub__
    return (self + (-other))
TypeError: bad operand type for unary -: 'tuple'    
(1/1)#这是第一个答案,非常有效。
回溯(最近一次呼叫最后一次):
文件“Fractions.py”,第36行,在
分数。打印(分数(5,6)-分数(1,6))
文件“Fractions.py”,第15行,在子部分__
返回(自+(-其他))
TypeError:一元操作数的操作数类型错误-:“tuple”

您将名称
self
分配给方法
self.\uuuu neg\uuuu
,在以下行中:

self = self.__neg__
other = other.__neg__
然后,将名称
other
分配给行中的方法
other.\uuuu neg\uuuu

self = self.__neg__
other = other.__neg__
因此,当您调用,
self.\uuuu add.\uuuu(other)
时,您正在尝试向分数添加一个方法

相反,使用

def __neg__(self):
    return Fraction(-self.numerator, self.denominator)

def __sub__(self, other):
    return self + -other

您将名称
self
分配给行中的方法
self.\uuuu neg\uuuu

self = self.__neg__
other = other.__neg__
然后,将名称
other
分配给行中的方法
other.\uuuu neg\uuuu

self = self.__neg__
other = other.__neg__
因此,当您调用,
self.\uuuu add.\uuuu(other)
时,您正在尝试向分数添加一个方法

相反,使用

def __neg__(self):
    return Fraction(-self.numerator, self.denominator)

def __sub__(self, other):
    return self + -other

这段代码有很多问题,但导致该错误消息的直接问题是,您的
\uuuu neg\uuuu
\uuuu sub\uuuu
函数需要调用
\uuu neg\uuuu
函数(
self.\uu neg\uuuuu()
,而不是
self.\uuu neg\uuu

下面是一个有效的代码版本(只要它能够解析并且不会引发异常):


这段代码有很多问题,但导致该错误消息的直接问题是,您的
\uuuu neg\uuuu
\uuuu sub\uuuu
函数需要调用
\uuu neg\uuuu
函数(
self.\uu neg\uuuuu()
,而不是
self.\uuu neg\uuu

下面是一个有效的代码版本(只要它能够解析并且不会引发异常):


运行此命令也有助于阐明:

def __neg__(self):
    print('self is a ' + str(type(self)))
    print('self.__neg__ is a ' + str(type(self.__neg__)))
    self = self.__neg__
    print('self is now a ' + str(type(self)))
    return self

运行此命令也有助于阐明:

def __neg__(self):
    print('self is a ' + str(type(self)))
    print('self.__neg__ is a ' + str(type(self.__neg__)))
    self = self.__neg__
    print('self is now a ' + str(type(self)))
    return self


你的
\uuuu init\uuuuu
方法在哪里?请修复你的缩进。对于大多数语言来说,这只是为了可读性,但错误的缩进会破坏Python代码。如果您的缩进在问题中是好的,那么人们可以更容易地运行、测试和再现代码中的错误。这使得回答您的问题更容易。您的
\uuuu init\uuuu
方法在哪里?请修复缩进。对于大多数语言来说,这只是为了可读性,但错误的缩进会破坏Python代码。如果您的缩进在问题中是好的,那么人们可以更容易地运行、测试和再现代码中的错误。这使得回答您的问题更容易。您需要
返回调用
\uuuuu add\uuuu
的结果。您也可以只执行
返回self+-other
,而不是显式调用
\uuuuu add\uuuuu
。@Blckknght更新为@Jared Goguen,在进行上述更改后,出现此错误:回溯(最近一次调用最后一次):'File“Fractions.py”,第37行,在Fraction.printtit(Fraction(Fraction(5,6)-Fraction(1,6))文件“Fractions.py”中,第17行,在sub-self+(-other)TypeError中:一元数的操作数类型错误-:“tuple”“@hiteshn97如果您发布完整的类,并使用适当的缩进,这会有所帮助。这段代码对我来说很好,所以问题就在别处。@Jaredoguen我已经发布了我的全部代码以及出现的
错误
。你需要
返回调用
\uuuuuuuu\uuuuu
的结果。您也可以只执行
返回self+-other
,而不是显式调用
\uuuuu add\uuuuu
。@Blckknght更新为@Jared Goguen,在进行上述更改后,出现此错误:回溯(最近一次调用最后一次):'File“Fractions.py”,第37行,在Fraction.printtit(Fraction(Fraction(5,6)-Fraction(1,6))文件“Fractions.py”中,第17行,在sub-self+(-other)TypeError中:一元数的操作数类型错误-:“tuple”“@hiteshn97如果您发布完整的类,并使用适当的缩进,这会有所帮助。这段代码对我来说很好,所以问题就在别处。@Jaredoguen我已经发布了我的全部代码以及出现的
错误
,这是我得到的
回溯错误(最近一次调用):
文件“Fractions2.py”,第37行,在
`Fraction.printti(Fraction(5,6)+Fraction(1,6))`
文件“Fractions2.py,第6行,在uuu init uuuuuuu
`self.numerator=numerator/g`
AttributeError:无法设置属性
您不能在我的实现中对属性进行变异,因为它们属于
namedtuple
,这实际上是一个Python
元组
(因此它是不可变的)。改变属性通常是不明智的。我建议编写一个不同的函数来进行规范化,而不是在构造函数中进行规范化。当构造函数的唯一责任是存储给定的属性时,代码往往更容易理解。这是我得到的错误回溯(上次调用):文件“Fractions2.py”,第37行,在`Fraction.printit(分数(5,6)+分数(1,6))`文件“Fractions2.py”,第6行,在uuu init uuuu
`self.numerator=numerator/g`
AttributeError:无法设置属性
您不能在我的实现中更改属性,因为它们属于
namedtuple
,这实际上是一个Python
元组
(因此它是不可变的)。改变属性通常是不明智的。我建议编写一个不同的函数来进行规范化,而不是在构造函数中进行规范化。当构造函数的唯一责任是