Kindly explain this source code for me

In summary, a shallow copy creates a new object, while a deep copy creates a copy of the original object.
  • #1
user366312
Gold Member
89
3
TL;DR Summary
The following source code is collected from stackoverflow.com. It demonstrates the design pattern to implement shallow copy and deep copy.
Check this link: How to override the copy/deep-copy operations for a Python object?

Can anyone explain, in layman's terms, what is going on in this source code?

copy functions in python:
from copy import copy, deepcopy

class MyClass(object):
    def __init__(self):
        print('init')
        self.v = 10
        self.z = [2, 3, 4]

    def __copy__(self): # why doesn't this function take any argument?
        cls = self.__class__  # Python equivalent of C++'s "this"-pointer
        result = cls.__new__(cls) # Python equivalent of C++'s static constructor. Why is it explicitly invoked here?
        result.__dict__.update(self.__dict__) # updating data-type information
        return result

    def __deepcopy__(self, memo): # what is memo? why is it needed?
        cls = self.__class__
        result = cls.__new__(cls)
        memo[id(self)] = result # what is going on here?
        for k, v in self.__dict__.items():
            setattr(result, k, deepcopy(v)) # what is going on here?
        return result

a = MyClass()
a.v = 11
b1 = copy(a) # why is this being called without object 'a'?
b2 = deepcopy(a)# why is this being called without object 'a'?
a.v = 12
a.z.append(5)
print(b1.v, b1.z)
print(b2.v, b2.z)
print(b2.v, b2.z)

The following is the output of the above source code:

Output:
Output:
init
11 [2, 3, 4, 5]
11 [2, 3, 4]

Why is 11 not changed into 12?

Why is element 5 missing the 3rd line?
 
Last edited:
Technology news on Phys.org
  • #2
This is actually quite involved for a beginner question.

If you are the one who has added the questions in comments, I can see your questions don't start with copy or deepcopy, but how classes and methods are declared. How familiar are you wth Python syntax?
 
  • Like
Likes jim mcnamara and berkeman
  • #3
Start from here:
Here is the basic syntax - which you can compare to your sample.
https://docs.python.org/3/library/copy.html

But, the way your question is written might imply that you should consider -

  • A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original.
  • A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original.
The difference is whether a reference to the original object (using a pointer) is created. Or the original(s) itself gets its very own copy. Why would this be useful?
 
  • Informative
Likes jack action

FAQ: Kindly explain this source code for me

What is the purpose of source code?

The purpose of source code is to provide a set of instructions and commands that a computer can understand and execute in order to perform a specific task or function.

What is the difference between source code and compiled code?

Source code is written by humans in a programming language, while compiled code is the result of the source code being translated into machine code that the computer can directly execute.

How do I read and understand source code?

To read and understand source code, you need to have a basic understanding of the programming language used, the syntax of the code, and the logic behind the code. You can also refer to documentation or seek help from others who are familiar with the code.

What are the benefits of having well-written source code?

Well-written source code is easy to understand and maintain, making it more efficient for debugging and making updates. It also makes it easier for other developers to collaborate and work on the code.

What are some common elements found in source code?

Some common elements found in source code include variables, loops, conditional statements, functions, and comments. These elements help to organize and control the flow of the code and make it easier to understand.

Similar threads

Replies
1
Views
3K
Replies
3
Views
2K
Replies
23
Views
2K
Replies
2
Views
2K
Replies
5
Views
2K
Replies
12
Views
1K
Replies
1
Views
1K
Back
Top