Retrieving the list of references to an object in Python

后端 未结 7 1243
栀梦
栀梦 2020-12-04 01:29

All:

a = 1
b = a
c = b

Now I want to get a list of object 1 tagged, which is [a, b, c]. How could I do this?

7条回答
  •  粉色の甜心
    2020-12-04 02:08

    It is not possible to find all references to a given object in Python. It is not even possible to find all objects or all references in Python. (The CPython function gc.get_objects does this, but it is not portable across Python implementations.)

    You can use dir() or locals() to find all variables that exist at some scope in the program. But if objects have been defined in other places, you could miss them with this method.

提交回复
热议问题