4 ms·
Your link just goes to the first page. Is this presumably the code you're referencing? import gc, sys def find_names(obj): frame = sys._getframe(1)
by MaxGabriel 14y ago
Your link just goes to the first page. Is this presumably the code you're referencing?
import gc, sys
def find_names(obj):
frame = sys._getframe(1)
while frame is not None:
frame.f_locals
frame = frame.f_back
result = set()
for referrer in gc.get_referrers(obj):
if isinstance(referrer, dict):
for k, v in referrer.iteritems():
if v is obj:
result.add(k)
return tuple(result)
- notaddicted 14y agoyup, thanks for the heads up, it worked for me. I realized that most of that was unnecessary, this is what I was talking about: import gc, sys def show(*args): for obj in args: frame = sys._getframe(1) for k,v in frame.f_locals.iteritems(): if v is obj: print "%s=%r" % (k,v) a, b, c, d, e, = False, 1, [1,2,3], {'x':2 }, map show(a, b, c, d, e) outputs: a=False b=1 c=[1, 2, 3] d={'x': 2} e=<built-in function map> [ http://codepad.org/4JogU069 http://codepad.org/4JogU069 ] [ edit: slight cleanup]
- irahul 14y agoNot so useful when you have shared pool. a = 10 b = 10 show(a) Or a = 'test' b = 'test' show(a)