Re: what is the use of weakref?

From: Duncan Booth (me_at_privacy.net)
Date: 01/30/04


Date: 30 Jan 2004 09:14:14 GMT


"ali" <rmangaliag@slu.edu.ph> wrote in
news:1jipe1-fqq.ln1@news.slu.edu.ph:

> i've seen a lot of times in programs but until now i still dont know
> the use of the weakref module... any help will be appreciated...
> thanks...
>

Normally in Python an object is never destroyed until you can no longer
reference it. Occasionally it can be convenient to hold onto an object, but
not to mind too much if it gets destroyed.

For example, say you had a word processing application with a complex data
structure for each paragraph style that was used in a document. When
someone changes the style of an existing paragraph, if it now matches the
style of another paragraph you want to share the same data structure rather
than creating a new one. What you could do here would be to make each
paragraph hold a strong reference to the required data structure, but also
store a weak value dictionary which maps the description of the paragraph
style onto the data structures.

With this kind of setup, the entries in the weak value dictionary always
exactly match the paragraph styles which are in use; as soon as a style is
no longer used it can be released, but you never need to duplicate the
complex data structure as a simple dictionary lookup will give you a
suitable one if it exists.

In short weak references help you to share or reuse data structures while
still allowing them to be freed automatically when no longer used.



Relevant Pages

  • Re: Weight loss.
    ... a witty comeback contains some sort of homosexual reference. ... paragraph and my thoughts in the paragraph below. ... My command of British English and Australian Outback is somewhat weak, ...
    (misc.fitness.weights)
  • Re: June Humour Awards nominations
    ... a witty comeback contains some sort of homosexual reference. ... paragraph and my thoughts in the paragraph below. ... My command of British English and Australian Outback is somewhat weak, ...
    (misc.fitness.weights)
  • Re: The 198
    ... evidences the "fact" that those characters profiled retain their mutant ... profiles are meant to represent characters remaining mutants after HOUSE OF M -- ... Yes, it seems I did make one claim about the book as a "reference piece," ... That's rather more reasonable than the previous paragraph. ...
    (rec.arts.comics.marvel.xbooks)
  • Re: page breaks auto-inserted and pages renumbered when going to p
    ... before/keep with next paragraph format options. ... inserting breaks immediately preceding a reference field (it would literally ... When printing to PDF, I found that only one reference had a break inserted ... The next step is to redefine the bookmarks for the cross-references to ...
    (microsoft.public.word.pagelayout)
  • Re: open() behavior under heavy disk load
    ... effects which may be created because of race conditions ... recently occured when a refence-counted data structure was supposed to ... represented while there was still an active transmission for this ... reference to the first data structure, too, because replies could come ...
    (comp.os.linux.development.system)