getting name of passed reference

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Tue Dec 29 02:04:16 EST 2009


On Mon, 28 Dec 2009 19:28:32 -0800, Joel Davis wrote:

> my thanks go out to Emile and Mr Hanson for their responses, I think
> I've found the solution, much shorter as well:
> 
>     > #!/usr/bin/python
> 
>     > import traceback
> 
>     > def testing ( varPassed ):
>     >         print traceback.extract_stack()[0][3]
> 
>     > testing("123")
> 
> and it seems the traceback module in general seems to have a lot of
> history to it. This project is for CPython so compatibility with Jython,
> Iron Python, et al isn't really that important right now. So as far as
> functionality and compatibility I think I'm set as long as
> traceback.extract_stack is 3.0 safe.


I'm afraid that one fails again. Do you actually test your solutions 
before telling us you've solved the problem?


>>> import traceback
>>> def testing ( varPassed ):
...     print traceback.extract_stack()[0][3]
...
>>>
>>> testing("123")
None
>>> x = "123"
>>> testing(x)
None

When a "solution" doesn't work under some circumstances (in this case, 
when run in the interactive interpreter) that's a warning that you need 
to understand when and where it will work before using it in production. 
Otherwise, how do you know that it will work under other circumstances?

Or, find an alternative. What are you actually trying to do? "Get the 
name of a passed reference" is a means to an end. What are you expecting 
to do with it?



-- 
Steven



More information about the Python-list mailing list