未绑定方法必须以实例作为第一个参数调用
unbound method must be called with instance as first argument
我正在尝试在 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!!!!!')
我不确定我是否制作了可以实例化的正确 class,但是我使用它时,Thefraction.add
在 __name__=='__main__'
的旁边。我错过了什么?
它的本意是这样的:
thefraction = Thefraction(a, b)
if choice == 1:
print(thefraction.add())
然后在你的 class:
def add(self):
return self.a + self.b
等等。不要在方法中包含 a
和 b
作为参数。
是的,再看一遍关于 classes 的教程。彻底。
如果你想像使用它一样使用它,你应该将你的class方法定义为@class方法,你不需要一个init:
class TheFraction:
@classmethod
def add(cls, a, b):
return a+b
这种声明方法的方式表明它们在 class 的实例中不是 运行 但可以这样调用:
TheFraction.add(a, b)
你还没有把 ()
放在你的 theFraction
对象前面。即使你这样做了,你也会面临另一场灾难。你用两个变量 (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!!!!!')
我正在尝试在 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!!!!!')
我不确定我是否制作了可以实例化的正确 class,但是我使用它时,Thefraction.add
在 __name__=='__main__'
的旁边。我错过了什么?
它的本意是这样的:
thefraction = Thefraction(a, b)
if choice == 1:
print(thefraction.add())
然后在你的 class:
def add(self):
return self.a + self.b
等等。不要在方法中包含 a
和 b
作为参数。
是的,再看一遍关于 classes 的教程。彻底。
如果你想像使用它一样使用它,你应该将你的class方法定义为@class方法,你不需要一个init:
class TheFraction:
@classmethod
def add(cls, a, b):
return a+b
这种声明方法的方式表明它们在 class 的实例中不是 运行 但可以这样调用:
TheFraction.add(a, b)
你还没有把 ()
放在你的 theFraction
对象前面。即使你这样做了,你也会面临另一场灾难。你用两个变量 (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!!!!!')