Python 必须以实例作为第一个参数调用unbound方法

Python 必须以实例作为第一个参数调用unbound方法,python,instantiation,Python,Instantiation,我正在尝试用python2.x构建简单的分数计算器 from fractions import Fraction class Thefraction: def __init__(self,a,b): self.a = a self.b =b def add(self): return a+b def subtract(self,a,b): return a-b def divide(self,a,b

我正在尝试用python2.x构建简单的分数计算器

from fractions import Fraction
class Thefraction:

    def __init__(self,a,b):
        self.a = a
        self.b =b
    def add(self):
        return a+b
    def subtract(self,a,b):
        return a-b
    def divide(self,a,b):
        return a/b
    def multiply(self,a,b):
        return a/b

if __name__=='__main__':
    try:
        a = Fraction(input('Please type first fraction '))
        b = Fraction(input('Please type second fraction '))
        choice = int(input('Please select one of these 1. add 2. subtract 3. divide 4. multiply '))
        if choice ==1:
            print(Thefraction.add(a,b))
        elif choice==2:
            print(Thefraction.subtract(a,b))
        elif choice==3:
            print(Thefraction.divide(a,b))
        elif choice==4:
            print(Thefraction.multiply(a,b))
    except ValueError:
        print('Value error!!!!!')

我不确定我是否创建了可以实例化的正确类,但是我使用了它,比如,
Thefraction.add
\uuuuuuu name\uuuuuuu==''uuuuu main\uuuuu'
的一侧。我错过什么了吗

应该这样做:

thefraction = Thefraction(a, b)
if choice == 1:
    print(thefraction.add())
然后在你们班上:

def add(self):
    return self.a + self.b
等等。不要将
a
b
作为参数包含在方法中


是的,再看一次关于课程的教程。彻底。

如果您想像使用它一样使用它,您应该将类方法定义为@classmethod,而不需要init:

class TheFraction:
    @classmethod
    def add(cls, a, b):
        return a+b
这种声明方法表明,它们不会在类的实例中运行,但可以像这样调用:

TheFraction.add(a, b)
您没有将
()
放在您的
分数对象前面。即使你这样做了,你也将面临另一场灾难。你用两个变量(a,b)初始化你的对象,这意味着你将像

Thefraction(a,b).add(a,b)
我不认为你想要这个,因为a
b
在每个方法中都是局部变量。。这是一种你不需要的变量。 我想你想要的是这个

  Thefraction(a,b).add()
这是完整的代码

from fractions import Fraction
class Thefraction:

    def __init__(self,a,b):
        self.a = a
        self.b =b
    def add(self):
        return self.a+ self.b
    def subtract(self):
        return self.a-self.b
    def divide(self):
        return self.a/self.b
    def multiply(self):
        return self.a/self.b

if __name__=='__main__':
    try:
        a = Fraction(input('Please type first fraction '))
        b = Fraction(input('Please type second fraction '))
        choice = int(input('Please select one of these 1. add 2. subtract 3. divide 4. multiply '))
        if choice ==1:
            print(Thefraction(a,b).add())
        elif choice==2:
            print(Thefraction(a,b).subtract())
        elif choice==3:
            print(Thefraction(a,b).divide())
        elif choice==4:
            print(Thefraction(a,b).multiply())
    except ValueError:
        print('Value error!!!!!')

你为什么用这样的方法?您是否考虑过学习结构化Python OOP教程?您可以使用
@staticmethod
修饰符并将a和b放入参数中进行添加。在使用
@staticmethod
修饰符时,您还可以删除
\u init\u
方法。在你放弃
\uuuu init\uuuu
方法的时候,你也可以让它们成为函数而不是方法。他不是问如何简化或改进他的代码,只是问如何正确实例化一个类。你可以,你只是错过了几个步骤!对于底层选民:很明显,OP是一个初学者,刚刚学习了课程,有点困惑,我只是想帮你。我不认为我在鼓励懒惰。我也知道,使用函数或静态方法可以更好地解决这个问题,但这不是重点:他们正在学习类的基础知识。请解释你将来的反对票,因为我怀疑他们是否像你认为的那样明显合理。感谢你的慷慨,这是巨大的动力和鼓励。OP仍然对课程的基本知识感到困惑,这只会让情况变得更糟。你可能是对的,你的答案是正确的,用init来做。我只是提供了一个替代方案,当我开始回答时,你的答案不在那里;)sheww..我离开后很久没有做过这么多编辑了,所以..嘻嘻..我想这基本上和上面的一样,只是@AlexHall被实例化了,不是吗?谢谢你花时间帮助我