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
Recommended Posts