x == y implies x is y in weakref proxies?

Quinn Dunkan quinn at hork.ugcs.caltech.edu
Tue Jan 22 18:28:57 EST 2002


I remember a little while back there was a discussion about this.  I recently
stumbled across:

% python2.2
Python 2.2c1 (#4, Dec 14 2001, 14:32:07) 
[GCC 2.95.2 19991024 (release)] on sunos5
Type "help", "copyright", "credits" or "license" for more information.
>>> class A: pass
... 
>>> a = A()
>>> import weakref
>>> p1 = weakref.proxy(a)
>>> p2 = weakref.proxy(a)
>>> p1 == p2
0
>>> p1 == a
0
>>> p1 is p2
1
>>> a == a
1

weakrefobject.c says:

/* Weak references only support equality, not ordering. Two weak references
   are equal if the underlying objects are equal. If the underlying object has
   gone away, they are equal if they are identical. */

Of course, that's weakrefs, not proxies.  But later on:

static int
proxy_compare(PyObject *proxy, PyObject *v)
{
    UNWRAP_I(proxy);
    UNWRAP_I(v);
    return PyObject_Compare(proxy, v);
}



The underlying object is equal to itself.  The code above looks like it's
comparing the underlying object.  I assume 'is' works because
PyWeakref_NewProxy "/* attempt[s] to return an existing weak reference if it
exists */".  Should '==' work too?



More information about the Python-list mailing list