0

I am asked to do a deep copy of an object if the recursive function does not automatically do so , how can one check?

And is there a way to use deep copy without calling the copy model ? Here is the function i want check def weird_list(n): if n==0: return [] res= weird_list(n-1) res.append(weird_list(n-1)) return res

31st Dec 2024, 8:20 AM
Dareen Moughrabi
Dareen Moughrabi - avatar
5 Réponses
+ 5
your function is not copying any list. It's just creating a recursively nested list of empty lists... quick google search result: https://blog.finxter.com/5-best-ways-to-copy-a-nested-list-in-JUMP_LINK__&&__python__&&__JUMP_LINK/
31st Dec 2024, 10:00 AM
Bob_Li
Bob_Li - avatar
+ 3
Bob_Li i have now made sure it actually does a deep copy as it is since each list inside a newly made list is saved at a diffrent location
31st Dec 2024, 10:56 AM
Dareen Moughrabi
Dareen Moughrabi - avatar
+ 2
You can check if your result is a deep copy by altering a deeply nested element of the original nested list and checking if your copy is affected. It should not change if the original is altered.
31st Dec 2024, 10:10 AM
Bob_Li
Bob_Li - avatar
+ 1
Bob_Li makes sense thank you ,I remeber a sentence from the lecture that sometimes recursion does a deep copy automatically , if i remember corectly then when is this the case?
31st Dec 2024, 10:04 AM
Dareen Moughrabi
Dareen Moughrabi - avatar
+ 1
Thank you very much , i will go and read about both 🙏🫡
31st Dec 2024, 10:16 AM
Dareen Moughrabi
Dareen Moughrabi - avatar