我尝试up
在 class 中使用 1 个参数定义函数X
,如下所示:
class X:
def __init__(self):
self.elem=[]
def i(self, new):
self.elem.append(new)
self.up(self,len(self.elem)-1) <= error here
def up(self, n):
print n
border = X()
a = [2,4,3,1]
for i in a:
border.i (i)
错误如下所示:
$ Traceback (most recent call last):
File "p.py", line 60, in <module>
border.i (i)
File "prim.py", line 50, in i
self.up(self,len(self.elem)-1)
TypeError: up() takes exactly 2 arguments (3 given)
$
如果我这样调用i
,self.up(self)
它会编译并print n
显示如下:
$ <__main__.X instance at 0x7f7a7ddf0128>
<__main__.X instance at 0x7f7a7ddf0128>
<__main__.X instance at 0x7f7a7ddf0128>
<__main__.X instance at 0x7f7a7ddf0128>
$