【发布时间】:2017-06-22 05:12:00
【问题描述】:
这是我的代码。我是 Python 的初学者。我尝试在func2 和func3 类Apple 中调用func。在我谷歌我的问题后,我发现它可以通过使用@staticmethod 来工作,但我想找出另一种不使用@staticmethod 的方法。在func3中,我没有使用@staticmethod,所以报错:print(mycall.func3(3) )
TypeError: func3() takes exactly 1 argument (2 given)
有谁知道如何解决我的问题?谢谢。
class mango:
def func(self):
a=8
b=9
return a+b
class Apple:
@staticmethod
def func2(x,y):
temp=mango()
c=temp.func()
z=x+y+c
return z
def func3(val):
temp = mango()
d = temp.func()
output = d + val
return output
if __name__ == '__main__':
print "Hello, Python!"
a = []
b =[]
ans = Test(a,b)
print(ans.cc)
mycall = Apple()
print(mycall.func2(2,3))
print(mycall.func3(3) )
【问题讨论】:
-
def funcx(self, par1, par2)将自身添加到类方法 -
顺便说一句:当您调用
mycall.func3(3)时,它的处理方式几乎与func3(mycall, 3)一样,因此您需要self才能获得mycall
标签: python