[Python-Dev] List mutation in list_repr?
patrickwesterhoff at gmail.com
Tue Dec 6 05:27:35 EST 2016
I just stumbled on the following comment in the C source of the repr
implementation for the list object:
/* Do repr() on each element. Note that this may mutate the list,
so must refetch the list size on each iteration. */
(as seen in list_repr implementation )
I’m honestly very surprised about this remark since I can neither
understand why this would be the case (repr shouldn’t mutate the
list), and I also don’t see any clue in the source as to when this
would actually happen. Since inside the loop, the list object `v` is
never accessed other than passing `v->ob_item[i]` to the recursive
repr call, there shouldn’t be any mutation on the list object itself.
I understand that a custom repr implementation could theoretically
mutate an object, but this could only affect the object itself (or its
children), but not the container list. So the list object `v` here
should be safe from mutations.
I tried looking at the original change when this was introduced.
Unfortunately, this was in 2001 , so I don’t really expect Tim to
still know *why* the comment was added back then.
Do you have any insights on why the comment is there, and whether I am
missing something that could actually mutate the list, making the size
refetch necessary and the comment justified?
Thanks a lot!
More information about the Python-Dev