Can “soft references” exist in Python?

孤人 提交于 2019-12-21 04:12:19

问题


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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/7331389/can-soft-references-exist-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!