Self variable changes when the variable it's assigned to changes

Marcel Kämper

I created the class Sorter which sets the variable self.list in the __init__ equal to a given argument. I then created a function selectionSort which should copy the value of self.list into a new variable unsortedList. That worked out, but when I then change unsortedList, the self.list variable changes as well. Here's my code:

class Sorter:
        def __init__(self, list):
                self.list = list

        def selectionSort(self):
                unsortedList = self.list
                sortedList = []
                indexSmallest = 0

                while len(unsortedList)>0:
                        for i in range(len(unsortedList)):
                                if unsortedList[i] <= unsortedList[indexSmallest]:
                                        indexSmallest = i
                        sortedList.append(unsortedList[indexSmallest])
                        unsortedList.pop(indexSmallest)
                        indexSmallest = 0
                return sortedList

sorter = Sorter([2,6,1,8,5])
print(sorter.selectionSort())

I expect self.list to be the same as before calling the selectionSort() function but the result I get is an empty self.list variable.

ycx

Use either:

#1
unsortedList = self.list.copy()

Or

#2
unsortedList = self.list[:]

Or

#3
import copy
unsortedList = copy.deepcopy(self.list)

Explanation:

When you do an assignment via =, it really is referring to the same list just that now that list has 2 different names.

To circumvent this, use #1 or #2 methods -> you would require the .copy() inbuilt function or using [:].

As for #3, this is used when shallow copying isn't enough because you might have mutable objects within the list itself.
For a greater understanding on copy vs deepcopy, visit and read here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

React state changes when its assigned variable changes

How to detect when a variable changes value

While loop should stop when variable changes

Execute a method when a variable value changes in Swift

does variable assigned to another variable changes( original variable changes ) does the second variable change?

How JQuery objects assigned to a variable detect changes?

change directive when scope variable changes

I've assigned Laravel Query Builder to a variable. It changes when being used

NgIf not updating when variable changes

Variable not being stored in the code when sheet changes

Refresh textbox when variable changes

Data is not updated when using as object, but changes normally when it's a variable

Detecting when a variable changes in javascript

Variable changes when it hasn't been changed

Angular: Check when Output Variable in Component Changes?

Assigned variable follows the changes of array unexpectedly

UIButton Changes Variable When Tapped?

Why does an element of an array assigned to a variable changes when the variable is changed?

gdb: stop the program when the variable changes

Bash variable changes when inside quotes

How to save variable when orientation changes

output shape of tensor changes when assigned to a variable

Python - Run function when a variable changes

Inner function does not return changes to variable assigned in outer function

Flutter execute a function when a value of a variable changes

Emit an event when a variable changes

Variable's value changes for no reason

Update variable when react state changes

variable assigned when it is assigned