Any way to stringify a variable id / symbol in Python?

Posted by otz on Stack Overflow See other posts from Stack Overflow or by otz
Published on 2010-06-08T19:27:11Z Indexed on 2010/06/08 19:32 UTC
Read the original article Hit count: 146

Filed under:

I'm wondering if it is possible at all in python to stringify variable id/symbol -- that is, a function that behaves as follows:

>>> symbol = 'whatever'
>>> symbol_name(symbol)
'symbol'

Now, it is easy to do it on a function or a class (if it is a direct reference to the object):

>>> def fn(): pass
>>> fn.func_name
'fn'

But I'm looking for a general method that works on all cases, even for indirect object references. I've thought of somehow using id(var), but no luck yet.

Is there any way to do it?

© Stack Overflow or respective owner

Related posts about python