This week, we discussed the finer points of things we were supposed to learn while coding assignment 2. One of these was deepcopying.
When you normally create an object in Python, you essentially give a 'name' to a reference to some object. So, when people write "a = 6", whenever the computer sees "a", it follows the pointer to the value 6.
When people want to create a copy of "a" (let's say it's "b"), the computer saves space by simply copying the reference. So, when the computer sees "b", it goes to "a", which goes to "6."
If we change "b" to some other value, it's not a big deal. However, if we change "a" to some other value, we unintentionally change "b" as well, since "b" isn't a unique value, it's simply a reference to "a". If "a" changes, "b" does as well.
Obviously, this can serve to be quite the issues. Sometimes, we need to manipulate an object, without messing with any copies of it we have created. This, in turn, is why we have deepcopy.
In reality, deepcopy is what a human would think of as a copy. However, in a large program where we may have hundreds (or even thousands!) of objects, and an equally large number of copies of these object strewn about, it can start to become very inefficient to have new memory allocated every time we want a new reference to an object.
However, sometimes we really do need that extra space, and as such, we need to distinguish it with a proper function. This in turn saves space and runtime, while still not constricting us in any sort of way.
No comments:
Post a Comment