[Python-Dev] Why aren't more things weak referencable

Raymond Hettinger python at rcn.com
Mon May 31 00:38:04 EDT 2004


 [Martin v. Löwis]
> What specific, real-life application do you
> have in mind that requires strings to be weakly-referencable?

FWIW, my program searches a virtual graph for a goal state.  Successor
nodes (board positions) are computed by a complex function and stored as
strings.  The successions of moves were recorded with a dictionary
linking the positions (long strings as both keys and values).  When the
graph eventually rules out a group of positions, I wanted the succession
chains all disappear on their own (i.e. if a board position becomes
unreachable when a better move is found, then the succession history for
that position becomes irrelevant).

Essentially, I used weak dictionaries as a cache to values that are
expensive to compute and take up (collectively) a lot of memory.  Having
that cache weakly referenced automated the process of clearing out
values that lost their relevance.

IMO, this use case is too rare to warrant adding weak ref support to
strings.  It just served to precipitate an attempted workaround using
"class Str(str): pass".  Christian then clarified why that didn't work
as expected.



> In addition to the reason Christian gave, one (conceptually more
> important) reason is that strings can't participate in cycles. Weak
> references were introduced as a mechanism to avoid creating cyclic
> structures, so that "backward" links could be made weak references.

Right, that was a principal use case.  Hence, the focus should be on
tuples rather than strings.  Likewise, most containers should be
candidates for weakref support unless it is just too expensive (i.e.
lists and tuples). Also, any subclass can potentially introduce back
references, so all subclasses should be fair game.  

Still, if cycles were the only issue, we would just let GC take care of
it.
There is one other purpose for weakrefs.  Even in the absence of cycles,
weakrefs let applications to cache and track objects without creating a
strong reference that keeps objects alive forever.  This is especially
useful for objects that were expensive to compute, expensive to create,
or external to the program in some way.  That was the rationale regexp
patterns, files, sockets, and arrays.



> I
> can't see a reason why Unicode objects should be weakly referencable
> (just as I can't see a reason for plain strings).

Unicode objects are like list and dict objects.  None of them are
directly weak referencable.  They have to be subclassed.  Then, they
need weak referencability as much as any other instance would.




Raymond




More information about the Python-Dev mailing list