[Tutor] quote + value of a variable

Tim Johnson tim@johnsons-web.com
Tue May 6 21:27:01 2003


* Alan Gauld <alan.gauld@blueyonder.co.uk> [030506 14:54]:
> >     I'd like to write a function that would take
> > a variable and print out both the name of and the
> > value.
> 
> Usually a bad idea but if it's for a debug function then that's 
> probably an acceptable exception to the rule. Why not check 
> how pdb does it?
 
  For debug only, yes.

> > >>> print 'mylist: ' , mylist
> > mylist:  [1, 2, 3]
> > 
> > Any ideas appreciated. Pointers to
> > documentations welcome.
> 
> My first guess would be to trawl the builtins directory and 
> test the id of your variable against the names in there.
> 
> something like:
> 
> for name in dir():
>     if __builtins__[name] is var: print name, var
> 
> Or however you access the builtins dictionary directly!

  This gives me something to work with.
  Thanks Alan

-- 
Tim Johnson <tim@johnsons-web.com>
      http://www.alaska-internet-solutions.com
      http://www.johnsons-web.com