我目前正在玩类和函数,因为我不熟悉python,我想知道如何addy(self, addx)调用addx.
class test:
def __init__(self, x):
self.x = x
def addx(self):
y = self.x + 10
return y
def addy(self, addx):
z = addx() + 10
return z
one = test(1)
print(one.addy())
Run Code Online (Sandbox Code Playgroud)
第15行,打印(one.addy())TypeError:addy()缺少1个必需的位置参数:'addx'进程以退出代码1结束
您需要self从类方法中调用.
self.addx()
addx此行上的参数也不应该存在:
def addy(self, addx):
我认为这就是你想要的:
class test:
def __init__(self, x):
self.x = x
def addx(self):
y = self.x + 10
return y
def addy(self):
z = self.addx() + 10
return z
one = test(1)
print(one.addy())
Run Code Online (Sandbox Code Playgroud)