0

If I have the ID of some object for example, how can I find out all the objects that are pointing to it?

petabyte
  • 1,487
  • 4
  • 15
  • 31

1 Answers1

1
import gc

class Node(object):
    def __init__(self, data):
        self.data = data
        self.next = None

if __name__ == "__main__":
    a = Node(5)
    a.next = Node(4)
    a.next.next = Node(3)
    # returns a list of dictionary of the object(s) referring to a.next.next
    diction = gc.get_referrers(a.next.next)[0]
    diction['next'] = None
    print a.next.next

The garbage collector has some neat functions

get_referrers(...)
    get_referrers(*objs) -> list
    Return the list of objects that directly refer to any of objs.

get_referents(...)
    get_referents(*objs) -> list
    Return the list of objects that are directly referred to by objs.
petabyte
  • 1,487
  • 4
  • 15
  • 31
  • 1
    I dont consider this a way to skirt around calling it a double linked list. This is basically just relying on the gc automatically tracking references instead of storing the reference on your object. It would technically be no different if you were storing the backward references in a separate dict of your own...or then...just simply storing them on the node object itself...hence...a double linked list. Its just playing a game of where the reference is managed. You will end up writing a "go backward" method around this and probably store it on the Node anyways. – jdi Mar 23 '12 at 00:20
  • Yea, my point is that I don't think it will go over well to present this as proof of the ability to implement a linked list that can also go backwards :-) – jdi Mar 23 '12 at 01:40