Assume I have an instance foo of some class Class with some variable foo.x. Now I want to produce a copy of foo:
bar = foo
when I now change bar.x, this also changes foo.x. How can I get a copy of foo, where I can change everything without changing the original foo? Creating a new instance of Class is not an option, because I need it in the state in which foo already is.
You need to use the module copy.
bar = copy.deepcopy(foo)
You can use deepcopy:
bar = deepcopy(foo)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With