I have the following setup:
def returnList(arg=["abc"]):
return arg
list1 = returnList()
list2 = returnList()
list2.append("def")
print("list1: " + " ".join(list1) + "\n" + "list2: " + " ".join(list2) + "\n")
print(id(list1))
print(id(list2))
Output:
list1: abc def
list2: abc def
140218365917160
140218365917160
I can see that arg=["abc"] returns copy of the same default list rather than create a new one every time.
I have tried doing
def returnList(arg=["abc"][:]):
and
def returnList(arg=list(["abc"])):
Is it possible to get a new list, or must I copy the list inside the method everytime I want to some kind of default value?