3 ms·
Everything is a first class object? What about the if-statement? Anyway, my personal gripes aside, I've been looking for something like this: http://www.scribd.
by notaddicted 14y ago
Everything is a first class object? What about the if-statement? Anyway, my personal gripes aside, I've been looking for something like this: http://www.scribd.com/doc/58306088/Bad-Ideas#outer_page_54 http://www.scribd.com/doc/58306088/Bad-Ideas#outer_page_54 ... a way to print "name=value" for variables for debugging.
- MaxGabriel 14y agoYour 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)