Get The Most Affordable Hosting in the World!
Starting at just $1.87/month, Vercaa offers unbeatable pricing for world-class web hosting services.
Fast, reliable, and secure hosting to power your website without breaking the bank. Plus, enjoy a free CDN for faster loading times worldwide!
Get Started Now!Since a variable in Python is merely a label or reference to an object in the memory, a simple assignment operator will not create copy of object.
Example 1
In this example, we have a dictionary "d1" and we assign it to another variable "d2". If "d1" is updated, the changes also reflect in "d2".
Output
id: 2215278891200 dict: {'a': 11, 'b': 22, 'c': 33} id: 2215278891200 dict: {'a': 11, 'b': 22, 'c': 33} id: 2215278891200 dict: {'a': 11, 'b': 100, 'c': 33} id: 2215278891200 dict: {'a': 11, 'b': 100, 'c': 33}
To avoid this, and make a shallow copy of a dictionary, use the copy() method instead of assignment.
Example 2
Output
When "d1" is updated, "d2" will not change now because "d2" is the copy of dictionary object, not merely a reference.
id: 1586671734976 dict: {'a': 11, 'b': 22, 'c': 33} id: 1586673973632 dict: {'a': 11, 'b': 22, 'c': 33} id: 1586671734976 dict: {'a': 11, 'b': 100, 'c': 33} id: 1586673973632 dict: {'a': 11, 'b': 22, 'c': 33}
The End! should you have any inquiries, we encourage you to reach out to the Vercaa Support Center without hesitation.