Background
I'm dealing a bit with recursion lately and I've always been dealing with global vs non global variables and memory stuff. In some of the my latest exercises I've noticed a big difference between lists and ints.
Let's look at the following code:
def foo(x):
x += 5
def bar(y):
y.append(2)
def thr(p):
p[0] += 2
if __name__ == '__main__':
x = 0
y = [1]
p = [3]
foo(x)
bar(y)
thr(p)
print(x)
print(y)
print(p)
output:
0
[1, 2]
[5]
Note how the int hasn't been affected but the list and items inside a list indeed have.
My Question
I know there's a huge difference between a list and an int. and from my experience with other languages (for example c) I can assume that the difference rises because when passing an int it is passed by value and a list is by reference.
Is that correct? why sometimes it seems like lists are passed by value and sometimes by reference (forcing the usage of things like lst.copy()?
Also, I'd like to know how can I pass an int by reference in python (without using something like putting it inside a list as the only element).