Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can "soft references" exist in Python?

In other languages (e.g. Java), object references can be Strong, Weak, Soft or Phantom (http://weblogs.java.net/blog/enicholas/archive/2006/05/understanding_w.html).

In Python, references are Strong by default and the WeakRef module allows weak references.

Is it possible to have "soft references" in Python?

In my particular case, I have a cache of objects that are time-consuming to create. Sometimes there may be no references to a cached object, but I don't want to throw the cached object away if I don't have to (i.e. if memory is plentiful).

like image 939
afaulconbridge Avatar asked Sep 07 '11 09:09

afaulconbridge


1 Answers

Python doesn't natively offer any flavors of references besides hard (aka strong) & weak.

That said, here is a softref implementation I whipped up a year or so ago which I've been using in a few places I needed one. What it provides aren't quite actual soft references, but it comes close for most use cases. It's a little rough around the edges, but is fully functional... though it relies on some reference counting internally that means it'll probably break on anything except CPython.

In particular, I wrote it precisely for a cache of expensive-to-create long-lived objects... the SoftValueDictionary should be exactly what you're looking for.

like image 162
Eli Collins Avatar answered Nov 18 '22 22:11

Eli Collins