Why changing places of what is on the left and right of = sign gives an error, and the other way it works?

https://www.codecademy.com/paths/computer-science/tracks/complex-data-structures/modules/cspath-hash-maps/lessons/hash-maps-implementation/exercises/defining-the-setter

Hello,

why in the exercise 2 the following line of code doesn’t work:

def assign(self, key, value):
    array_index = self.compressor(self.hash(key))
    value = self.array[array_index]

but this one does:

def assign(self, key, value):
    array_index = self.compressor(self.hash(key))
    self.array[array_index] = value

Why is it important that value is on the right and not on the left of = sign?

Thank you.

Hi @ai-2090,

The purpose of the assign method of the HashMap class is to place a value, which is represented by the value parameter, into an appropriate location within the HashMap instance represented by self. However, your first code example simply overwrites the value parameter with whatever is already at that location in the HashMap instance’s array list. Upon termination of the method’s execution, nothing has changed in the HashMap instance, and value is unsaved.

The second code example works by copying the value of value into the appropriate place within the HashMap instance’s array list.

5 Likes

Thank you sir, now it is much more clear.

3 Likes