Is the weakref.ref callback is called deterministically?

Jane Austine janeaustine50 at hotmail.com
Thu Jun 13 08:51:04 EDT 2002


Dear Pythonistas,  

I'm studying weakref module recently to make a variation of WeakList.

In Andres Tuells' WeakList[1] and weakref module, I have found
something interesting. There are "while 1" loops in fetch methods.

[weakref module]
    def popitem(self):
        while 1:
            key, value = self.data.popitem()
            o = key()
            if o is not None:
                return o, value

[WeakList module]
	def __getitem__(self, i):
		while 1:
			result  = self.data[i]()
			if result is not None:
				return result

Even though the index i does not change, it loops till the
result is not None. Seemingly it depends on the weakref.ref
callback to remove dead weakrefs.

However, the while loop wouldn't be needed if the callback
was called right after the last reference to the object
was "del"ed.

Is it that we can't expect exactly when the weakref.ref callback
will be called?

With warmest regards,

 Jane Austine.
------------------------------------
[1] http://sourceforge.net/tracker/download.php?group_id=5470&atid=305470&file_id=14021&aid=487738



More information about the Python-list mailing list