To do the equivalent of Ruby's ObjectSpace._id2ref in Python

Object ID-> To entity ...

Extract reference from object ID There is something called ObjectSpace._id2ref in Ruby,

class Hoge
  attr_accessor :uhihi
end

h = Hoge.new
h.uhihi = 300
h.__id__ # 70211856425920
ObjectSpace._id2ref(70211856425920).uhihi # = 300

In Python, from the object ID obtained by id (variable) How can I take the object itself as a reference? As soon as I thought about it, StackOverflow Post came out.

import gc

class Hoge(object):
  pass

h = Hoge()
h.uhihi = 300
id(h) # 4342077776

reference = None
for obj in gc.get_objects():
  if id(h) == id(obj):
    reference = obj

reference.uhihi # = 300

gc.get_objects?

Recommended Posts

To do the equivalent of Ruby's ObjectSpace._id2ref in Python
How to get the number of digits in Python
[Python] How to do PCA in Python
In the python command python points to python3.8
Check the behavior of destructor in Python
How to determine the existence of a selenium element in Python
How to know the internal structure of an object in Python
How to do R chartr () in Python
[Python] PCA scratch in the example of "Introduction to multivariate analysis"
[Cloudian # 9] Try to display the metadata of the object in Python (boto3)
How to check the memory size of a variable in Python
Feel free to change the label of the legend in Seaborn in python
I wrote the code to write the code of Brainf * ck in python
How to check the memory size of a dictionary in Python
The basics of running NoxPlayer in Python
In search of the fastest FizzBuzz in Python
Solve the smallest value in Python (equivalent to paiza rank D)
What to do when the value type is ambiguous in Python?
I want to batch convert the result of "string" .split () in Python
I want to explain the abstract class (ABCmeta) of Python in detail.
[Introduction to Python] Thorough explanation of the character string type used in Python!
An example of the answer to the reference question of the study session. In python.
How to use the C library in Python
Output the number of CPU cores in Python
I want to do Dunnett's test in Python
[Python] Sort the list of pathlib.Path in natural sort
Summary of how to import files in Python 3
Get the caller of a function in Python
View the result of geometry processing in Python
Minimal implementation to do Union Find in Python
Make a copy of the list in Python
Summary of how to use MNIST in Python
Draw graphs in Julia ... Leave the graphs to Python
Find the divisor of the value entered in python
The trick to write flatten concisely in python
How to get the files in the [Python] folder
Find the solution of the nth-order equation in python
The story of reading HSPICE data in Python
[Note] About the role of underscore "_" in Python
About the behavior of Model.get_or_create () of peewee in Python
Solving the equation of motion in Python (odeint)
Output in the form of a python array
What to do to get google spreadsheet in python
I want to display the progress in Python!
the zen of Python
[Python] What to do when PEP8 is violated in the process of importing from the directory added to sys.path
Various ways to read the last line of a csv file in Python
How to pass the execution result of a shell command in a list in Python
I want to use Python in the environment of pyenv + pipenv on Windows 10
[python] What to do when an error occurs in send_keys of headless chrome
Visualize the timeline of the number of issues on GitHub assigned to you in Python
How to find the coefficient of the trendline that passes through the vertices in Python
How to get a list of files in the same directory with python
Easy way to check the source of Python modules
python beginners tried to predict the number of criminals
How to retrieve the nth largest value in Python
I tried to graph the packages installed in Python
Experience the good calculation efficiency of vectorization in Python
The wall of changing the Django service from Python 2.7 to Python 3
How to get the variable name itself in python
Solve addition (equivalent to paiza rank D) in Python