Python List Aliasing

Asked 2 years ago, Updated 2 years ago, 51 views

This code changes the list

Why doesn't this code change the list? I don't know the difference between the two

python list

2022-09-21 17:26

1 Answers

In the second case, the value letters goes into the delete_head function and receives it in a place called t (approximately t=letters), and processes something under the name t. We changed the value of t within it, but none of the letters were manipulated when delete_head ended.

In the first case, t contains letters, so t and letters are connected to the same array object. So, if you apply the del function del called array object that t points to, array object, the letters are erased exactly as it was erased in t. Because it's connected.

In fact, if you apply a code that changes the value of t differently before you run delt[0] in the code of delete_head, the letters will not be affected.

It would be helpful to study call by value / call by reference that is accessed in other popular programming languages, and also to find out call by object reference that is only spoken in Python. There are actually some problems with what I explained.

Call by object reference I will add a link to what some people have organized. https://item4.github.io/2015-07-18/Some-Ambiguousness-in-Python-Tutorial-Call-by-What/
http://hyunalee.tistory.com/43


2022-09-21 17:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.