what is the use of weakref?

A

ali

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...

ali
 
M

Mike C. Fletcher

Well, for the last few versions of Python, it's primary use has been
finding ways to crash the interpreter ;) . Seriously, they are used
when you want to avoid having cycles in your data structures, so that,
for instance, variables will be immediately garbage collected rather
than hanging around waiting for the garbage collector to find them.

They are also useful in some decoration patterns where you want to cache
compiled values for a large number of objects with uncertain lifetimes.
If you were to use hard-refs to the client objects from the cache you'd
keep the objects alive (presumably the cache is reachable all the time).
A weak-ref allows the client objects to go away so that the cached
values can be cleared and the memory reclaimed (OpenGLContext does this
with compiled scenegraph node representations, btw).

HTH,
Mike
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...

ali
_______________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://members.rogers.com/mcfletch/
 
D

Duncan Booth

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top