priya
priya

Reputation: 91

How slice operator workd internally to prevent side effect while copyting variables

I read this article - http://www.python-course.eu/deep_copy.php. But I could not understand how internally [:] works to prevent the side effect or I should take it as it is the compiler feature. ?

Upvotes: 0

Views: 35

Answers (1)

Amber
Amber

Reputation: 526573

Slices in Python use copies of data, rather than the original data.

Using x = y[:] makes an actual copy, rather than just assigning a reference. A slice in Python is a separate copy of the data, and a [:] slice is one that contains the entire set of data being sliced.

So x = y just says "make x point to the same sequence as y" but x = y[:] says "make x point to a copy of all of the data in the sequence that y points to."

Upvotes: 1

Related Questions