WeakValueDictionary

Ken Seehof kseehof at neuralintegrator.com
Thu Sep 27 19:35:00 EDT 2001


> Ken Seehof wrote:
> 
> >  >>> sys.version
> > '2.1.1 (#20, Jul 20 2001, 01:19:29) [MSC 32 bit (Intel)]'
> >  >>> import weakref
> >  >>> d = weakref.WeakValueDictionary()
> >  >>> d['z']
> > Traceback (most recent call last):
> >   File "<input>", line 1, in ?
> >   File "C:\PYTHON21\lib\weakref.py", line 35, in __getitem__
> >     o = self.data.get(key)()
> > TypeError: object of type 'None' is not callable
> > 
> > Shouldn't this be raising KeyError instead of TypeError?
> 
> Probably :)
> 
> While we are at it, I would like to see the ability to have a weak-ref 
> to None - this would allow me to remove special casing for None in the 
> places I have used weakrefs to date.
> 
> But-that-is-a-different-problem ly,
> 
> Mark.

Well here's how I got around the weak None thing:

def NoneRef():
    return None

Then you can say x=NoneRef and x will act like a weak-ref to None.
(yeah, I know return None is the same as pass, but I like being explicit)

Whaddya-think-should-I-patent-it-ly yrs,

- Ken






More information about the Python-list mailing list