Weak references (- E04 - Leadership! Google, Guido van Rossum, PSF)

M.-A. Lemburg mal at egenix.com
Fri Jan 13 20:20:49 CET 2006


Duncan Booth wrote:
> Alex Martelli wrote:
> 
>> It IS true that in Python you cannot set arbitrary attributes on
>> arbitrary objects.  The workaround is to use a dict, indexed by the id
>> of the object you want to "set arbitrary attributes on"; this has the
>> helpful consequence that separate namespaces are used, so your arbitrary
>> setting of metadata cannot interfere with the `true' attributes of the
>> object in question.
>>
> That's a horrible suggestion (using id's, not the bit about separate 
> namespaces). If you use the id then attributes will persist beyond the 
> lifetime of the object and may suddenly reappear on other unrelated objects 
> later.
> 
> A better suggestion here would be to use weak references. Unfortunately, 
> not every Python object can be the target of a weak reference, so there is 
> a limitation here preventing a useful implementation for many builtin 
> types.

mxProxy could help with that:

   http://www.egenix.com/files/python/mxProxy.html

It allows creating weak references to any Python object
(among other things like protecting object access).

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Jan 13 2006)
>>> Python/Zope Consulting and Support ...        http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ...             http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
________________________________________________________________________

::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,FreeBSD for free ! ::::



More information about the Python-list mailing list