Can you obtain names from parent scope?

Will Sadkin wsadkin at nameconnector.com
Fri Jan 17 10:51:35 EST 2003


I want to write a function that takes an arbitrary set of objects,
and prints them in the form "name = <value>."  That is, I want to be
able to say:

>>> a = 'hi!'
>>> dbgval(a)
a = hi!
>>> b = 2
>>> dbgval(b)
b = 2

However I can't make this work, because objects don't have any
intrinsic name, and if I write a function of the form
>>> def dbgval(*args):
    ...

then dbgval() only knows the arguments objects as belonging to the
list 'args', and I can't find any way to determine what the names
were in the parent's local scope when the function was called.
Is there any way for a function to retrieve this information?

Thanks in advance,
/Will Sadkin
Parlance Corporation




More information about the Python-list mailing list