I have the following problem: I have noticed that when I pass a list to a function and I append some values to that list, not just the list inside the function is modified but also the original list. However, it is well known that this does not happen to "common" variables. Here there is some code that you can try on your own. I used classes because I'm using them for a specific application, but you can forget :
class HasLeak():
def __init__(self):
self.tmp_list = []
class MainClass():
def __init__(self):
return
def main_function(self,object_):
object_.tmp_list.append(1)
return
def main_function_list(self,variable):
variable.append(1)
return
def main_function_variable(self,variable):
variable+=1
return
if __name__=='__main__':
object_leak = HasLeak()
print (object_leak.tmp_list)
mc = MainClass()
mc.main_function(object_leak)
print (object_leak.tmp_list)
list_ = []
print (list_)
mc.main_function_list(list_)
print (list_)
variable = 0
print (variable)
mc.main_function_variable(variable)
print (variable)
The output obtained is
[]
[1]
[]
[1]
0
0
while I would expect
[]
[]
[]
[]
0
0
Is this a normal behaviour in python? How can I avoid it?