473,386 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

weakref and memoizing

This is yet another memoize decorator, it's meant to be resilient (and
fast enough):
http://aspn.activestate.com/ASPN/Coo.../Recipe/466320

Like most memoize decorators it stores the pairs of data-result in
cache dictionary, but Garrett Rooney says:

it could be better if it used the new weak references from python 2.1.
The way it works now, the cached values will be stored forever, and
will never be garbage collected. This is fine if they are small, but if
they happen to be non trivial objects, the memory cost could be a
problem. If they are refered to with weak references, they can still be
garbage collected, as long as there are no strong references to them.

So maybe I can use a dict with weak values:
self._cache = weakref.WeakValueDictionary()
But the keys of such dict can't be ints and others. Maybe here someone
can suggest me if and how weak references can be used in this
situation.

Bye,
bearophile

Jan 20 '06 #1
0 1070

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mark | last post by:
Hello all, Peter Norvig has a great recipe for memoizing Python functions available at http://www.norvig.com/python-iaq.html . I made good use of this until I tried to memoize a generator. At...
1
by: Ames Andreas (MPA/DF) | last post by:
Hi all, I'm using python 2.1 and can't easily upgrade (Zope). I'm using the Queue module to synchronize/communicate between two threads and weakref.proxy objects to avoid cycles. Scenario: ...
2
by: ali | last post by:
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
1
by: Mathias Mamsch | last post by:
Hi, I have some confusion concerning the weakref module. I am trying to save a weak reference to a bound member function of a class instance for using it as a callback function. But I always...
2
by: Mike C. Fletcher | last post by:
I'm looking at rewriting parts of Twisted and TwistedSNMP to eliminate __del__ methods (and the memory leaks they create). Looking at the docs for 2.3's weakref.ref, there's no mention of whether...
6
by: Daishi Harada | last post by:
Hi, I'm trying to find/write a memoizing decorator that works both for functions and methods. I've been looking at the following two samples: ...
3
by: Odalrick | last post by:
I'm trying to write a simple game and decided I need an eventmanager. <code> import weakref from collections import defaultdict class _EventManager( object ): def __init__( self ):...
6
by: George Sakkis | last post by:
I'm baffled with a situation that involves: 1) an instance of some class that defines __del__, 2) a thread which is created, started and referenced by that instance, and 3) a weakref proxy to the...
1
by: Ripter | last post by:
I found this script at http://www.pygame.org/wiki/LazyImageLoading?parent=CookBook And I can't quite figure out how it works. I was wondering if someone could clarify it for me. The Code is: ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.