[Tutor] quote + value of a variable

Alan Gauld alan.gauld@blueyonder.co.uk
Tue May 6 18:52:03 2003


>     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?

> >>> 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!

Alan G.