3

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?

2
  • 3
    possible duplicate of python, can i print original var name? Commented Jun 8, 2010 at 19:39
  • I'm looking at the actual source code of NameError in python26/Python/ceval.c as obviously NameError does get the symbol name right. (As I noted below, symbol_name(non_existent) yields a traceback with "NameError: name 'non_existent' is not defined" message.) PyTuple_GetItem (and PyDict_GetItem) yields the symbol name I want, but I haven't found an interface that wraps it and reveals it to the user. So unless someone proves me otherwise, it may well be impossible. For any instance variable of a class, it is possible by overriding getattribute and grepping NameError, however. Commented Jun 8, 2010 at 20:35

3 Answers 3

4

Here is, I'm sure you can turn it into a better form =)

def symbol_name(a):
    for k,v in globals().items():
        if id(a)==id(v): return k

Update: As unbeli has noted, if you have:

a = []
b = a

The function will not be able to show you the right name, since id(a)==id(b).

Sign up to request clarification or add additional context in comments.

6 Comments

>>> symbol_name(a) 'a' >>> b=a >>> symbol_name(b) 'a' oops
Given that any object can have multiple names, wouldn't this be slightly better: if a is v: results.append(k) ... (assuming you wrap the loop in an instantiation and return of "results").
still cannot tell the difference between two equal variables. Obviously, if id(x)==id(y), then symbol_name(x) == symbol_name(y), but should be 'x' != 'y'
Are you trying to provide debugging information? Might you consider using the traceback module? If you're trying to report "the name" of an object you might have to search through locals() and globals() and ... I'm not sure sure what other name spaces.
unbeli is right, it works not for all cases, if two variables have the same id, it may return the wrong name.
|
3

I don't think it's possible. Even for functions, that is not the variable name:

>>> def fn(): pass
... 
>>> fn.func_name
'fn'
>>> b=fn
>>> b.func_name
'fn'
>>> del fn
>>> b.func_name
'fn'
>>> b()
>>> fn()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'fn' is not defined

Comments

-1

"Not possible" is the answer.

1 Comment

Might be partially possible answer here: stackoverflow.com/a/9121991/134044 which in turn refers to a "yes it is possible" answer (with caveats) here: stackoverflow.com/a/8875313/134044

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.