Kindly explain this source code for me

Click For Summary
SUMMARY

This discussion focuses on overriding copy and deep-copy operations in Python using the copy and deepcopy functions from the copy module. The source code demonstrates the implementation of the __copy__ and __deepcopy__ methods within a custom class, MyClass. Key points include the distinction between shallow and deep copies, where shallow copies reference the original objects, while deep copies create entirely new instances. The output illustrates how changes to the original object do not affect the shallow copy but do affect the deep copy.

PREREQUISITES
  • Understanding of Python classes and methods
  • Familiarity with the copy module in Python
  • Knowledge of shallow vs. deep copying concepts
  • Basic understanding of Python object references
NEXT STEPS
  • Study the copy module documentation in Python
  • Learn about Python object references and memory management
  • Explore advanced class design patterns in Python
  • Investigate the implications of shallow vs. deep copies in data structures
USEFUL FOR

Python developers, software engineers, and anyone interested in understanding object copying mechanisms in Python for effective memory management and class design.

user366312
Gold Member
Messages
88
Reaction score
3
TL;DR
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?

[CODE lang="python" title="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)
[/CODE]

The following is the output of the above source code:

Output:
[CODE lang="python" title="Output"]
init
11 [2, 3, 4, 5]
11 [2, 3, 4]
[/CODE]

Why is 11 not changed into 12?

Why is element 5 missing the 3rd line?
 
Last edited:
Technology news on Phys.org
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   Reactions: jim mcnamara and berkeman
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   Reactions: jack action

Similar threads

  • · Replies 34 ·
2
Replies
34
Views
5K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 23 ·
Replies
23
Views
2K
Replies
3
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K