I am missing some understanding here and while I'm certain that this is in the Python documentation somewhere, I simply don't know where to look precisely.
I am trying to understand what is actually happening when I pass an argument to a function.
In other words... am I passing the object itself, am I passing a reference to the object, or am I merely passing the value.
Take the following simple example:
class Test:
def __init__(self):
self.val = 0
def inc(self):
self.val += 1
def set(self, val):
self.val = val
def __str__(self):
return str(self.val)
def sub(i,j,k):
i.inc()
j += 1
k = [x+1 for x in k]
print 'print1',i,j,k
i.set(5)
j = 5
k = [5]*3
print 'print2',i,j,k
another(i,j,k)
print 'print4',i,j,k
def another(l,m,n):
l.inc()
m += 1
n[0] = n[0]+1
print 'print3',l,m,n
def main():
a = Test()
b = 0
c = [0]*3
sub(a,b,c)
print 'print5',a,b,c
if __name__ == "__main__":
main()
The output is pretty straight forward...
print1 1 1 [1, 1, 1]
print2 5 5 [5, 5, 5]
print3 6 6 [6, 5, 5]
print4 6 5 [6, 5, 5]
print5 6 0 [0, 0, 0]
I just want to understand what ever fine line I'm somehow crossing here. I understand that class methods modify the object directly, but that would mean that I'm passing actual object, and not just its value/content. However, that's clearly not true for simple intergers, and even lists, but if I modify a list element, then ... magic ???