-1

I was reading this post and now am trying to create a class to modify a dictionary and store the modified dictionary in a new variable, without any effect on the original dictionary. Here is the code:

dict_1 = {'a':[0,1,2]}
print('dict_1 before updating is :', dict_1)
#################################################

class dict_working:
    def __init__(self, my_dict):
        self.my_dict = (my_dict)

    def update_dict(self):
        for i in range(len(self.my_dict['a'])) :
           self.my_dict['a'][i] = self.my_dict['a'][i]+10
        return self.my_dict
#################################################

obj = dict_working(dict_1)
b_dict = obj.update_dict()

print('dict_1 after updating is :', dict_1)
print('A new object after updating is:', b_dict)

But when I try the code, It also modifies the dict_1 variable:

dict_1 before updating is : {'a': [0, 1, 2]}
dict_1 after updating is : {'a': [10, 11, 12]}
A new object after updating is: {'a': [10, 11, 12]}

So I tried to deepcopy the dictionary in the class and the results was just as I wanted:

import copy
dict_1 = {'a':[0,1,2]}
print('dict_1 before updating is :', dict_1)
#################################################

class dict_working:
    def __init__(self, my_dict):
        self.my_dict = copy.deepcopy(my_dict)

    def update_dict(self):
        for i in range(len(self.my_dict['a'])) :
           self.my_dict['a'][i] = self.my_dict['a'][i]+10
        return self.my_dict

obj = dict_working(dict_1)
b_dict = obj.update_dict()
#################################################
print('After adding deepcopy method:')
print('dict_1 after updating is :', dict_1)
print('A new object after updating is:', b_dict)

The results are:

dict_1 before updating is : {'a': [0, 1, 2]}
After adding deepcopy method:
dict_1 after updating is : {'a': [0, 1, 2]}
A new object after updating is: {'a': [10, 11, 12]}

My question: Do Python experts also use the same deepcopy method in these situations or there are other ways?

Muser
  • 593
  • 1
  • 9
  • 23
  • This is the general way, if you'll need a deep-copy, yes – juanpa.arrivillaga Dec 25 '18 at 08:46
  • Technically you could leverage structural-sharing, if your objects are immutable (usually because you share references to them) you can assume they'll never change and only ever care about differences or "edits". There is no easy way in Python but it is possible with some effort. https://en.wikipedia.org/wiki/Persistent_data_structure – Reut Sharabani Dec 25 '18 at 08:52
  • It is not easy with the features that python offers. But you can use other libraries to achieve this result. In fact most use cases of databases I came across is for this. – kaushik94 Dec 25 '18 at 08:56

1 Answers1

1

The alternative is to take into account what kind of operation you're performing on the object and copy it in a way such that that operation won't affect the original. For example, in your case, you're not just operating on a dictionary; you're operating on a dictionary of lists, and modifying the lists, so you'd need to copy as far down as the elements of the lists. However, if those elements themselves were also structures, you wouldn't have to copy their content as well.

Of course, doing this relies on you being very aware of what your code does. It's easy enough here but in more complicated systems it can get very difficult to keep track. For cases like that, deepcopy() is a perfectly valid approach.

David Z
  • 128,184
  • 27
  • 255
  • 279