the cycle gc collector can be executed when any gctracked object is allocated e g during a call to pylistnew pydictnew moreover it can invoke arbitrary python code via a weakref callback this means that there are many places in the source where an arbitrary mutation could unexpectedly occur the example below shows listslice not expecting the call to pylistnew to mutate the input list of course there are many more examples like this one the cycle gc collector can be executed when any gc tracked object is allocated e g during a call to pylist_new pydict_new moreover it can invoke arbitrary python code via a weakref callback this means that there are many places in the source where an arbitrary mutation could unexpectedly occur the example below shows list_slice not expecting the call to pylist_new to mutate the input list of course there are many more examples like this one
import weakref

class A(object):
    pass

def callback(x):
    del lst[:]


keepalive = []

for i in range(100):
    lst = [str(i)]
    a = A()
    a.cycle = a
    keepalive.append(weakref.ref(a, callback))
    del a
    while lst:
        keepalive.append(lst[:])