Python's gc module has several useful functions, but it sounds like gc.get_referrers() is what you're looking for. Here's an example:

import gc


def foo():
    a = [2, 4, 6]
    b = [1, 4, 7]

    l = [a, b]
    d = dict(a=a)
    return l, d

l, d = foo()
r1 = gc.get_referrers(l[0])
r2 = gc.get_referrers(l[1])

print r1
print r2

When I run that, I see the following output:

[[[2, 4, 6], [1, 4, 7]], {'a': [2, 4, 6]}]
[[[2, 4, 6], [1, 4, 7]]]

You can see that the first line is l and d, and the second line is just l.

In my brief experiments, I've found that the results are not always this clean. Interned strings and tuples, for example, have more referrers than you would expect.

Answer from Don Kirkby on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › how to find reference to an object using gc.get_referrers()? it returns dictionary instead of the object
r/learnpython on Reddit: How to find reference to an object using gc.get_referrers()? It returns dictionary instead of the object
February 14, 2022 -

I have a linked list

node_0 = Node(11)
node_1 = Node(22)
node_2 = Node(33)
node_3 = Node(44)

node_0.next_node = node_1
node_1.next_node = node_2
node_2.next_node = node_3
linked_list_obj = LinkedList(node_0)

In this problem, you only have access to node_2 and I want to delete node_2 without changing values between nodes.

In linked list, if you want to delete node_2, you have to access to node_1.

So I thought I can use gc.get_referrers(node_2) to get the object of node_1

node_previous = gc.get_referrers(node_2) "return the list of objects" according to Python doc

print(node_previous[0])

>> {'data': 22, 'next_node': <Chapter14.Chapter14Q1.Node object at 0x0000020A74881F40>}

But that is a dictionary that logged the data of node_1. It's not actually not node_1.

Because if you print node_1, you get the actual object.
print(node_1)

>> <Chapter14.Chapter14Q1.Node object at 0x0000020A74881F70>

I want gc.get_referrers(node_2) to return the actual object (node_1) not the log of the object in a dictionary. Is this possible?

Discussions

Is it possible to get all the objects referencing a given object in Python? - Stack Overflow
Also, getting the line of code ... the object was created - I just don't know who is referencing it at the moment. P.S. I need this to debug a leak. ... I don't think Python keeps a list of the actual references; probably no reference-counting language keeps such a list. ... @lanzz: Don't forget that CPython isn't purely reference-counted. It has a secondary garbage collector that deals with reference cycles. ... What are you really trying to do? Suppose you find out this ... More on stackoverflow.com
🌐 stackoverflow.com
Python object references - Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most. Learn more about Collectives ... Connect and share knowledge within a single location that is structured and easy to search. Learn more about Teams ... I'm aware that in python every identifier or variable name is a reference to the actual object... More on stackoverflow.com
🌐 stackoverflow.com
Newest Questions - Stack Overflow
Stack Overflow | The World’s Largest Online Community for Developers More on stackoverflow.com
🌐 stackoverflow.com
List of object references in python - Stack Overflow
The behavior is because of exactly what you said: b holds a reference to an object. It does not hold hold a reference to the name you used to refer to that object. So the object in b[0] does not know anything about any variable called a. Assigning a new value to a has no effect on b. ... Find the ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Python Tutorial
pythontutorial.net › home › advanced python › python references
Python References
March 27, 2025 - When you access the counter variable, ... To find the memory address of an object referenced by a variable, you pass the variable to the built-in id() function....
🌐
Runestone Academy
runestone.academy › ns › books › published › fopp › TransformingSequences › ObjectsandReferences.html
9.4. Objects and References — Foundations of Python Programming
The answer is True. This tells us that both a and b refer to the same object, and that it is the second of the two reference diagrams that describes the relationship. Python assigns every object a unique id and when we ask a is b what python is really doing is checking to see if id(a) == id(b).
🌐
Real Python
realpython.com › videos › object-references
Object References (Video) – Real Python
And even things, like, that you might not expect—like, a function is also an object in Python. 00:56 So just, everything’s an object, ha. Keep that in mind, and you’ll see why it’s important in just a second. So, what happens when we do this standard variable assignment that we talked about before? We’re saying n = 300. 01:08 Let’s head over to the IPython shell and just give it a try, put it in here, n = 300. Now I can reference 300 with the variable name n.
Published: December 10, 2019
Find elsewhere
🌐
Towards Data Science
towardsdatascience.com › home › latest › understanding reference counting in python
Understanding Reference Counting in Python | Towards Data Science
January 22, 2025 - Reference counting is one of the memory management technique in which the objects are deallocated when there is no reference to them in a program. Let’s try to understand with examples. Variables in Python are just the references to the objects in the memory.
🌐
Finxter
blog.finxter.com › home › learn python blog › how to get the current reference count of an object in python?
How to Get The Current Reference Count of an Object in Python? - Be on the Right Side of Change
December 20, 2020 - You don’t have to care about the concrete memory locations of your objects and Python automatically performs garbage collection—that is—removing unused objects from memory. To do all of this work, Python relies on its own structures. Among those, are helper variables that may refer to an object you’ve created.
🌐
Medium
medium.com › @officialyrohanrokade › simplified-pythons-reference-handling-understanding-object-referencing-deleting-references-and-ae4026a74d7b
Simplified Python’s Reference Handling: Understanding Object Referencing, Deleting References, and Shallow Copying vs. Deep Copying | by Rohan Rokade | Medium
December 8, 2023 - Python employs a reference-based memory management system. Deleting references doesn’t immediately remove objects from memory; it decrements their reference counts. Assigning one object to the other doesn’t spawn a new object; it establishes another reference to the same object.
🌐
Better Programming
betterprogramming.pub › a-guide-to-reference-counting-in-python-27334fc2e3c1
A Guide to Reference Counting in Python | by Halil Yıldırım | Better Programming
May 4, 2020 - I see some people using sys.getrefcount(var) without knowing passing the var add one more reference to the object. Let’s look at the example below. Check that out! Output 3 while we expect 2(x and y). It happens because passing x to getrefcount function added one more reference. You can use the built-in ctypes module to find the result you expect.
🌐
LabEx
labex.io › tutorials › python-how-to-understand-object-references-in-python-398254
How to understand object references in Python | LabEx
When you create an object in Python, the interpreter allocates memory for that object and returns a reference to it.
🌐
Runestone Academy
runestone.academy › ns › books › published › thinkcspy › Lists › ObjectsandReferences.html
10.10. Objects and References — How to Think like a Computer Scientist: Interactive Edition
In other words, a list is a collection of references to objects. Interestingly, even though a and b are two different lists (two different collections of references), the integer object 81 is shared by both. Like strings, integers are also immutable so Python optimizes and lets everyone share the same object for some commonly used small integers.
🌐
Codementor
codementor.io › community › variable references in python
Variable references in Python | Codementor
April 22, 2019 - For c=2, a new object is again ... the reference counts of every object in python by importing the python library module-sys and accessing its function getrefcount....
🌐
Python Forum
python-forum.io › thread-40115.html
how to return a reference to an object?
June 5, 2023 - i have a collection of object references in a dict inside a function. i want to have a function that can use give arguments, find the object, and return a reference to it. i am drawing a blank trying to think this up. i want to have the caller inc...
🌐
Medium
medium.com › geekculture › python-reference-e6458a9a0582
Python — Reference
August 9, 2022 - Variables in Python have a special attribute: identity, that is, “identity identification”. This special property is also called “reference” in many places. Let’s first take a look at one example: in languages ​​such as C++, variable declaration and assignment can be separated: ... If you directly use a variable that does not exist, an error will occur, NameError: name ‘a’ is not defined · In Python, when a = 343 is executed, it first creates the object 343 in memory, and then let a point to it, which is the reference.
🌐
Linux Manual Page
linux.die.net › diveintopython › html › power_of_introspection › getattr.html
4.4. Getting Object References With getattr
>>> import odbchelper >>> odbchelper.buildConnectionString <function buildConnectionString at 00D18DD4> >>> getattr(odbchelper, "buildConnectionString") <function buildConnectionString at 00D18DD4> >>> object = odbchelper >>> method = "buildConnectionString" >>> getattr(object, method) <function buildConnectionString at 00D18DD4> >>> type(getattr(object, method)) <type 'function'> >>> import types >>> type(getattr(object, method)) == types.FunctionType True >>> callable(getattr(object, method)) True · A common usage pattern of getattr is as a dispatcher. For example, if you had a program that could output data in a variety of different formats, you could define separate functions for each output format and use a single dispatch function to call the right one.