If I have the ID of some object for example, how can I find out all the objects that are pointing to it?
Asked
Active
Viewed 242 times
0
-
1You can't, at least, not within the Python environment. – Amber Mar 22 '12 at 00:42
-
What are you hoping to accomplish with such information? – Karl Knechtel Mar 22 '12 at 00:43
-
1http://stackoverflow.com/questions/1396668/python-get-object-by-id – John Mar 22 '12 at 00:43
-
In a linked list, I want to be able to go backwards – petabyte Mar 22 '12 at 00:48
-
1That's what a double linked list is for. Why are you using a linked list in Python? – John La Rooy Mar 22 '12 at 00:55
-
I actually learned linked lists today and asked my prof if it's possible to go backwards and he said no (unless it's a double linked list), so I wanted to prove him wrong :p – petabyte Mar 22 '12 at 01:15
-
get_referrers(...) get_referrers(*objs) -> list Return the list of objects that directly refer to any of objs. – petabyte Mar 22 '12 at 03:42
-
Out of curiosity, was the discussion in your class about linked list specifically about python, or was it a general CS discussion regarding linked lists? – jdi Mar 23 '12 at 00:13
-
We talked about it generally and then also implemented functions in python – petabyte Mar 23 '12 at 01:30
1 Answers
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
-
1I 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