getting name of passed reference

Emile van Sebille emile at fenx.com
Mon Dec 28 20:10:35 EST 2009


On 12/28/2009 3:54 PM Joel Davis said...
> I'm just curious if anyone knows of a way to get the variable name of
> a reference passed to the function.

For curiosity, sure -- but it's real weak...

> Put another way, in the example:
>
>    def MyFunc ( varPassed ):
>       print varPassed;
>
>    MyFunc(nwVar)
>
> how would I get the string "nwVar" from inside of "MyFunc"?

Here are some ways of doing this:
http://www.python-forum.org/pythonforum/viewtopic.php?f=14&t=12659

> is it possible?

Yes -- but only for a limited set of situations.  Consider the following:

MyFunc(3)

a=[1,2,3,4]
MyFunc(a[2])

b=a
MyFunc(b[2])

def newval(): return 3
MyFunc(newval())

MyFunc(range(3)[-1])

Emile




More information about the Python-list mailing list