Can "soft references" exist in Python?
Asked Answered
C

2

11

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

Circumsolar answered 7/9, 2011 at 9:13 Comment(1)
A
6

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.

Angeliqueangelis answered 7/9, 2011 at 11:3 Comment(4)
@JamesBlackburn My apologies for that, I pulled that script out of my dev folder without much thought. I've updated the file to include a BSD license.Angeliqueangelis
This is also a great answer to my question. Without recompiling python with a different implementation of C's malloc, I don't think anything better is even possible.Sheatfish
The link to your softref implementation is broken.Megrim
The link is back to saying "(c) 2010-2011 Assurance Technologies, LLC" and nothing about a BSD licenseSussex
C
0

Another option is to use a cache that maintains a certain number of objects (e.g. 100) rather than explicitly calculating their memory consumption. When an object is accessed, it is put to the top of the cache if it exists, or the object on the bottom of the cache is replaced with the new object.

Untested, but it should work in theory.

Circumsolar answered 19/9, 2011 at 13:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.