Accessing the name of an actual parameter

Gary Herron gherron at islandtraining.com
Tue Jan 26 16:50:31 CET 2010


Hellmut Weber wrote:
> Hi,
>
> consider the following piece of code, please
>
> ----- -----
>
> def f(param):
>   nameOfParam = ???
>   # here I want to access the name of the variable
>   # which was given as parameter to the function
>   print nameOfParam, param
>   return
>
> if __name__ == __main__:
>
>   a = 1
>   f(a)
>
>   b = 'abcd'
>   f(a)
>
> ----- -----
>
> The output should be:
>
> 'a' 1
> 'b' 'abcd'
>
> ----- -----
>
> I tried to look at globals() and locals(), gave a look to the frames
> (sys._getframe(0) and sys._getframe(1),
> but did not see a possibility to access the information a want
>
> How can this be done?
>
> TIA
>
> Hellmut
>
>

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. 

Cheers,
Gary Herron




More information about the Python-list mailing list