Accessing the name of an actual parameter

Duncan Booth duncan.booth at invalid.invalid
Tue Jan 26 17:13:16 CET 2010

Gary Herron <gherron at> wrote:

> It's naive to think this question even makes sense.  There are many ways 
> f can be called which don't involve a parameter:
> f(42)
> f(time())
> f(a+123)
> f(sin(a))
> f(f(1))
> and so on. 

So long as the OP doesn't care if they get no names or several name that 
might not matter. However, explicitly passing in the name you want to use 
is likely to be more useful.

>>> def getcallersnames(obj):
	f = inspect.currentframe().f_back.f_back
	return [name for name in f.f_locals if f.f_locals[name] is obj]

>>> def bip(x):
	print getcallersnames(x)

>>> def foo(bar):
	baz = bar

>>> foo(3)
['bar', 'baz']

>>> bip(bip)

Duncan Booth

More information about the Python-list mailing list