我试图将一个方法从另一个文件调用到另一个文件中,该方法已导入,但调用时表示未定义self参数。我是一个初学者程序员,需要帮助:(
类别:
class ArithmeticMethod:
def __init__(self):
print('Arithmetic Class Init.')
def addition(self, value1, value2):
answer = int(value1) + int(value2)
return answer
def subtract(self, value1, value2):
answer = int(value1) - int(value2)
return answer
def multiply(self, value1, value2):
answer = int(value1) * int(value2)
return answer
def divide(self, value1, value2):
answer = int(value1) / int(value2)
return answer
电话:
from arithmetic import ArithmeticMethod
print(ArithmeticMethod.addition(value1=10, value2=9))
错误: 未绑定方法callpylint中的参数“self”没有值(参数没有值)
当您这样做时:
您是说
addition
方法与实例绑定(因为self
是第一个参数)。因此,当您调用它时,您必须构造一个实例来调用它:显然,这很愚蠢,因为您不需要任何实例,而且
self
参数未使用。所以,去掉它并用@staticmethod
注释它:现在,您可以将其作为静态方法调用,而无需通过实例:
将文件命名为
arithmetic.py
只需定义方法,所有方法对类都是静态的,与类无关
将其用于:
或
相关问题 更多 >
编程相关推荐