2

While analyzing python list I got surprised when I saw something which is not possible in other programming language. Lets say I have a list called my_list

my_list = [1,2,3,4,5,6,7,8,9,10] 

Next when I'm doing like

my_list[9] = my_list  #I didn't seen such things possible in C/C++

And when I'm printing my_list, my_list[9] and my_list[9][9], all these gives the same results.

my_list
[1, 2, 3, 4, 5, 6, 7, 8, 9, [...]]
my_list[9]
[1, 2, 3, 4, 5, 6, 7, 8, 9, [...]]
my_list[9][9]
[1, 2, 3, 4, 5, 6, 7, 8, 9, [...]]

What I understood is that my_list[9] is referring to same list called my_list and [...] means self referential list i.e list pointing to same list as when you do type(my_list[9]) it gives me type as list.

[1, 2, 3, 4, 5, 6, 7, 8, 9, [...]]
                              |
             Does it like self referential struct pointer concept of C ?

Above my_list example I just added for simple test run. I want to know how things like my_list[9] = my_list makes python code performance better. what is the actual intention behind this my_list[9] = my_list making possible in python ?

Any help will be greatly appreciated.

Achal
  • 11,821
  • 2
  • 15
  • 37
  • 1
    I can understand the dupe but is there a practical purpose for this being supported? – roganjosh Jun 20 '18 at 19:11
  • 1
    @roganjosh I don't see how it can be prevented while inserting... – Jean-François Fabre Jun 20 '18 at 19:13
  • @Jean-FrançoisFabre So it's a quirk rather than an actual feature that could ever find practical use? That's what I was thinking anyway, I was just curious if this was actively permitted for some use that I couldn't see. – roganjosh Jun 20 '18 at 19:17
  • 1
    this could lead to infinite loops on, for instance, a recursive flatten algorithm. But if the recursion is controlled, it could be useful. And python allows to store every reference in a list, including its own (cannot be checked, that would be too costly) – Jean-François Fabre Jun 20 '18 at 19:19
  • Question heading may be same, I agree @Jean-FrançoisFabre but my intention of this question was to know what python wants to achieve by making it possible `my_list[9] = my_list`. since `list` is mutable so its possible or it can contains any data type. – Achal Jun 20 '18 at 19:25
  • 1
    okay but the intention doesn't show in the question. Re-reading the question, and it fits the duplicate very much. Maybe you should clarify – Jean-François Fabre Jun 20 '18 at 19:30

2 Answers2

3

It's possible because a list (like other containers) store references, and why not a reference of itself?

The __str__ / __repr__ functions have been protected against this to avoid infinite recursion, and show an ellipsis instead (...).

Why is it possible? Because it's not impossible. If Python were to prevent that, it would mean checking for self-referencing every time an object is added in the list. This would either be an additional O(n) storage overhead for maintaining a reference cache, or an O(n) time overhead for performing a reference search.

wim
  • 338,267
  • 99
  • 616
  • 750
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
2

Lists in python don't contain anything except references to objects (like a pointer). They can refer to any object including themselves.

nosklo
  • 217,122
  • 57
  • 293
  • 297
  • Thanks for quick reply. So you are saying `list` actually doesn't contains values, it contains reference of those values ? – Achal Jun 20 '18 at 19:10
  • Python doesn't have values - in python everything are references - including normal variables, attributes, function parameters... – nosklo Jun 20 '18 at 19:11
  • Thanks. I understood it doesn't store values in the list, it stores the references to those values. Can you get me deeper into intention behind python supporting self-referential list as by doing `my_list[9] = my_list` actual `list` got modified, that's not actual self referential concept of other programming language. – Achal Jun 20 '18 at 19:57
  • it is not an intention or a special support - it works because there is no special case, everything is an object so everything can be stored in a list, including the list itself – nosklo Jun 20 '18 at 20:20