【问题标题】:unbound method must be called with instance as first argument必须以实例作为第一个参数调用未绑定的方法
【发布时间】:2016-06-10 22:57:28
【问题描述】:

我正在尝试在 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!!!!!')

我不确定我是否创建了可以实例化的正确类,但是我在__name__=='__main__' 旁边使用了Thefraction.add。我错过了什么?

【问题讨论】:

  • 你为什么要使用这样的方法?您是否考虑过遵循结构化 Python OOP 教程?
  • 您可以使用 @staticmethod 装饰器并将 a 和 b 放入参数中以进行添加。
  • 在你使用@staticmethod装饰器的时候,你也可以放弃__init__方法。在您删除 __init__ 方法时,您不妨将它们设为函数而不是方法。
  • 他不是在问如何简化或改进他的代码,而是在问如何正确地实例化一个类。
  • 可以,只是少了几步!

标签: python instantiation


【解决方案1】:

应该是这样的:

thefraction = Thefraction(a, b)
if choice == 1:
    print(thefraction.add())

然后在你的课堂上:

def add(self):
    return self.a + self.b

等等。不要在方法中包含 ab 作为参数。

是的,请再次阅读有关课程的教程。彻底。

【讨论】:

  • 致反对者:很明显,OP 是一个刚刚了解课程并且有点困惑的初学者,我只是想提供帮助。我不相信我在鼓励懒惰。我也知道用函数或静态方法可以更好地解决这个问题,但这不是重点:他们正在学习类的基础知识。请在未来解释您的反对意见,因为我怀疑他们是否像您认为的那样明显合理。
  • 感谢您的慷慨,这是很大的动力和鼓励。
【解决方案2】:

您尚未将 () 放在您的 theFraction 对象前面。即使你这样做了,你也会面临另一场灾难。你用两个变量(a,b)初始化你的对象,这意味着你会像

一样调用你的对象
Thefraction(a,b).add(a,b)

我不认为你想要这个,因为 ab 是每个方法中的局部变量。这是一种你不需要的变量。 我假设你想要的是这个。

  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!!!!!')

【讨论】:

  • sheww..自从我离开 SO..hee hee...
  • 我认为这与上面的基本相同,除了 @AlexHall 实例化,不是吗?感谢您花时间帮助我
【解决方案3】:

如果你想像使用它一样使用它,你应该将你的类方法定义为@classmethod,并且你不需要初始化:

class TheFraction:
    @classmethod
    def add(cls, a, b):
        return a+b

这种声明方法的方式表明它们不会在类的实例中运行,但可以这样调用:

TheFraction.add(a, b)

【讨论】:

  • OP 仍然对类的基础知识感到困惑,这只会让情况变得更糟。
  • 您可能是对的,您的答案是使用初始化的正确方法。我只是提供了一个替代方案,当我开始回答时,你的答案不存在;)
猜你喜欢
  • 2014-09-10
  • 1970-01-01
  • 2014-12-10
  • 1970-01-01
  • 2017-05-10
  • 2018-05-10
  • 1970-01-01
  • 2015-11-07
  • 1970-01-01
相关资源
最近更新 更多