Observer pattern thoughts
Mitch Chapman
Mitch.Chapman at bioreason.com
Fri Mar 7 12:01:12 EST 2003
Bernhard Herzog wrote:
> Bound methods for the same method of the same object are equal even if
> the bound methods are not identical:
[snip]
Is this true for ZODB Persistent objects? I've had trouble
unregistering Persistent callables because they sometimes seem not to
compare equal. (This has happened under Python 2.1.3 and
StandaloneZODB-1.0, so things may have changed.) Looking at the
workaround code, it seems that the methods may not compare equal
even when the instances do.
Here's roughly what the workaround looks like. It defines a
wrapper to be used as the registered callable:
class PersistentCB:
def __init__(self, persistentInstance, methodName):
self._instance = persistentInstance
self._methodName = methodName
self.__call__ = getattr(self._instance, self._methodName)
def __cmp__(self, other):
try:
return (cmp(self._instance, other._instance) or
cmp(self._methodName, other._methodName))
except AttributeError:
return cmp(id(self), id(other))
Instances of PersistentCB are registered on Callbacks and
Observables which unregister the last registered instance of
a callback. (Hm. I don't know why I let the same callable be
registered multiple times on the same callback.)
class Callback:
def unregister(self, cb):
...
# '==' works, but 'is' sometimes does not. (?)
if cb == self.callbacks[i]:
del self.callbacks[i]
return
Can someone familiar with the internals of ZODB and Persistent
explain what's going on here?
--
Mitch
More information about the Python-list
mailing list