I try to define the function up with 1 parameter in class X, like this:
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)
The error looks like this:
$ 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)
$
if I call in i like this self.up(self) , it compiles, and print n displays so:
$ <__main__.X instance at 0x7f7a7ddf0128>
<__main__.X instance at 0x7f7a7ddf0128>
<__main__.X instance at 0x7f7a7ddf0128>
<__main__.X instance at 0x7f7a7ddf0128>
$
You do not need to pass
selfon toup():Python does this for you; by looking up
self.up, you are given a bound method, which means that Python will automatically add inselfwhen you callup().You were, in effect, calling
up(self, self, len(self.elem)-1); three elements where only 2 were expected.