getting name of passed reference

Stephen Hansen apt.shansen at gmail.com
Mon Dec 28 19:11:52 EST 2009


On Mon, Dec 28, 2009 at 3:54 PM, Joel Davis <callmeclaudius at gmail.com>wrote:

> I'm just curious if anyone knows of a way to get the variable name of
> a reference passed to the function.
>
> Put another way, in the example:
>
>  def MyFunc ( varPassed ):
>     print varPassed;
>
>  MyFunc(nwVar)
>
> how would I get the string "nwVar" from inside of "MyFunc"? is it
> possible?
>

You can't. Well, some frame hackery might be possible, but it'll be really
complicated and messy and dangerous.

Objects have no idea what their various names are; those names are just tags
put attached to them, those tags being living various namespaces
(dictionaries).

Python doesn't really have pass-by-reference or pass-by-value, it doesn't
even have references. It has objects and names, and that's it. Objects are
the real data, and that's what you're passing into the function-- objects
have no idea what names they have, and its not really possible to find out
-- even in a local context, let alone peeking outside.

When Python evaluates "nwVar", it uses that name to get an object, and from
that point the name is forgotten. The object is passed into MyFunc, and
given another name-- varPassed-- and that's that.

HTH,

--S
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20091228/6245039d/attachment-0001.html>


More information about the Python-list mailing list