0

I have this bit of code

def find():
temp = liked
shuffle(temp)
if temp == liked:
    return True

By debugging I've seen that after shuffle(temp) also the liked variable changes. Why does it happen?

sickleox2
  • 35
  • 6
  • 2
    Because `temp` and `liked` are different names for the same object. – molbdnilo Aug 17 '21 at 12:23
  • Yes but why does the "liked" variable change if i change temp... – sickleox2 Aug 17 '21 at 12:23
  • So how do I create a copy of liked not linked to that object – sickleox2 Aug 17 '21 at 12:24
  • This should actually error out, as `liked` does not exist. The provided snippet seems to be incomplete. The behaviour you describe depends on the data type you're referencing. It is likely copied by reference not copied by value. – SvenTUM Aug 17 '21 at 12:28

1 Answers1

0

temp = liked is an assignment. It points temp to liked so that when you say shuffle(temp), temp is just referring shuffle() back to liked.

assignment: temp = liked

copy: temp = liked.copy()

You will find an in depth conversation about it here: Does Python make a copy of objects on assignment?

j__carlson
  • 1,346
  • 3
  • 12
  • 20