Just look into the disassembled code:
In [1]: def initializer():
...: root = [] # root of the circular doubly linked list
...: root[:] = [root, root, None, None]
...:
In [2]:
In [2]: import dis
In [3]: dis.dis(initializer)
2 0 BUILD_LIST 0
2 STORE_FAST 0 (root)
3 4 LOAD_FAST 0 (root)
6 LOAD_FAST 0 (root)
8 LOAD_CONST 0 (None)
10 LOAD_CONST 0 (None)
12 BUILD_LIST 4
14 LOAD_FAST 0 (root)
16 LOAD_CONST 0 (None)
18 LOAD_CONST 0 (None)
20 BUILD_SLICE 2
22 STORE_SUBSCR
24 LOAD_CONST 0 (None)
26 RETURN_VALUE
What you looking for is STORE_SUBSCR
op code which is there to implement the following:
mplements TOS1[TOS] = TOS2
Which is due the do documentation an In-place operations. And if you wonder what's the In-place operations, here's how the doc defines it:
In-place operations are like binary operations, in that they remove TOS and TOS1, and push the result back on the stack, but the operation is done in-place when TOS1 supports it, and the resulting TOS may be (but does not have to be) the original TOS1.
This will verify what the inline doc in the source code says:
initialize by pointing to self.
Regarding your other questions:
Is there a different syntax to achieve the same result?
Yeah you can as it's mentioned in other answer clear and set the list items using list.extend
attribute. Or assign the items one by one maybe lol
Is there a different common use case for some_list[:] =
some_iterable (without the self reference)?
This is a very vague question 'cause it is what it is. Assigning items in an Injective manner which could have the benefit of replacing items without recreating references, etc.
[:]
is actually a clever way to replace the entirety of the list without changing its reference. Soroot[:] = [root, root, None, None]
is not readily reproducible without a handful ofappend
andremove
ifroot
is more than an empty list. – Derian