【发布时间】:2017-11-11 17:23:31
【问题描述】:
我对 python 3(和一般编程)非常陌生,我在理解为什么会发生这种情况时遇到了一些问题。
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)
谁能提供一些关于为什么会发生这种情况的见解?有没有更好的方法来做到这一点?
【问题讨论】:
-
所有函数都需要添加self
-
或者每个方法上面的装饰器
@staticmethod -
主要问题是:为什么这是一个类?
标签: python python-3.x