[ZODB-Dev] weakref and Persistent

Tim Peters tim at zope.com
Wed Apr 23 17:38:19 EDT 2003


[Phillip J. Eby]
> ...
> So, I guess this should be a good indication that persistent objects will
> be potentially weakref-able under ZODB4.  (I say potentially, because
> Persistent subclasses that define '__slots__' and don't inherit from an
> already weakref-able base class, will not be weakref-able.)

I'm not sure exactly what you have in mind there, but if you have a specific
example, try adding the string '__weakrefs__' to the class's __slots__.
That's usually all it takes (Python needs an attr named __weakrefs__ to
implement weak references, and if you use __slots__ then Python can't create
any new attrs by magic; but you can create the __weakrefs__ slot yourself
then).

Example:

Python 2.2.2 (#37, Oct 14 2002, 17:02:34) [MSC 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from weakref import ref
>>> class A(object):
...     __slots__ = 'a', 'b', 'c'
...
>>> a = A()
>>> ref(a)    # can't do it -- Python can't create a new __weakrefs__ attr
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: cannot create weak reference to 'A' object
>>> class B(object):   # but if you create a __weakref__ slot, no problem
...     __slots__ = 'a', 'b', 'c', '__weakref__'
...
>>> b = B()
>>> ref(b)
<weakref at 0x798030; to 'B' at 0x798070>
>>> _()
<__main__.B object at 0x00798070>
>>>




More information about the ZODB-Dev mailing list