0

Need Help! I want the frut list to not get changed when changing the attribute of an object A.

https://code.sololearn.com/cdKqc6Bq7J5B/?ref=app

14th Feb 2022, 9:54 AM
Pratham
Pratham - avatar
2 Respuestas
+ 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 :)
14th Feb 2022, 10:04 AM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 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!
14th Feb 2022, 10:30 AM
Pratham
Pratham - avatar