0
Need Help! I want the frut list to not get changed when changing the attribute of an object A.
2 Antworten
+ 7
That happens because you are dealing with references only. If you want the list in frut to be its own list, not a reference to A's list, then you can create a copy:
frut.append(A.fruits_list.copy())
Or by passing the list to the list constructor (kind of saying create a list from this list)
frut.append(list(A.fruits_list))
Edit: another suggested possibility is to use += instead of .append(). An explanation as to why that works would be appreciated :)
+ 1
Thanks very much,
I was also thinking of a way to take the static attribute value of an object instead of its references when appending to frut list.
that's good your methods work!