Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.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 函数接受2个参数,但给出了3个?_Python_Python 3.x - Fatal编程技术网

Python 函数接受2个参数,但给出了3个?

Python 函数接受2个参数,但给出了3个?,python,python-3.x,Python,Python 3.x,我对Python3(以及一般的编程)非常陌生,我在理解为什么会发生这种情况时遇到了一些问题 class calculator: def addition(x, y): added = x + y print(added) def subtraction(x, y): sub = x - y print(sub) def multiplication(x, y): mult = x * y

我对Python3(以及一般的编程)非常陌生,我在理解为什么会发生这种情况时遇到了一些问题

class calculator:

    def addition(x, y):
        added = x + y
        print(added)

    def subtraction(x, y):
        sub = x - y
        print(sub)

    def multiplication(x, y):
        mult = x * y
        print(mult)

    def division(x, y):
        div = x / y
        print(div)

calc = calculator()

calc.multiplication(3,5)
我得到了这个问题这个问题:

Traceback (most recent call last):
File "/Users/JordanM/Desktop/PythonFiles/Calculator.py", line 20, in <module>
calc.multiplication(3,5)

TypeError: multiplication() takes exactly 2 arguments (3 given)
回溯(最近一次呼叫最后一次):
文件“/Users/JordanM/Desktop/PythonFiles/Calculator.py”,第20行,在
计算乘法(3,5)
TypeError:乘法()正好接受2个参数(给定3个)

有人能解释一下为什么会发生这种情况吗?有没有更好的方法可以做到这一点?

您需要将self添加到实例使用的所有函数中。在javascript中是这样的,它指向当前实例

   def addition(self,x, y):
        added = x + y
        print(added)

您需要将self添加到所有函数或每个方法上方的decorator
@staticmethod
。主要问题是:为什么这是一个类?