How to get the name of a variable

Alex Martelli aleaxit at yahoo.com
Fri Nov 3 08:11:22 EST 2000


<laskowsk at my-deja.com> wrote in message news:8tttfn$qna$1 at nnrp1.deja.com...
> How can i get the name of a python variable from c++ when i have the
> PyObject* of it ??

In general, you can't -- there could be any number of "names"
for that object, be it 0, 1, or, say, 23, after all.

Suppose your C++ function is named 'whatname' and consider
what results you want it to see in various cases:


class Plok: pass

whatname(Plok()) # no name at all

def afun():
    a = Plok()
    whatname(a)  # one name (local)

def bfun():
    global b
    b = Plok()
    whatname(b)  # one name (global)

def abfun():
    global b
    a = b = Plok()
    whatname(b)  # two names (one global, one local)

dic = {}
dic['what ho!'] = Plok()
whatname(dic.get('what ho!'))    # is this a 'name'...?

vec = []
vec.appen(Plok())
whatname(vec[0])                 # what about this...?

plik = Plok()
plik.pluk = Plok()
whatname(plik.pluk)              # and this...?


As you can see, your question is not very well posed.  In
general, a PyObject* does not have "one name" -- it has a
totally arbitrary non-empty set of references to it (if
the set became empty, the object would go away...), some
of which may be values in dictionaries (while others may
not be -- they may be items in lists, for example); some
of the values-in-dictionaries may correspond to what you
think of as "a name" (presumably, this will hold for
entries in the __builtins__, globals() and locals()
dictionaries, whose keys match your criteria for being
"names") while others may not correspond (entries in
other dictionaries, for example -- but it's hard to say
what you WANT 'name' to mean, e.g., is 'plik.pluk' a
name?  that one is the entry for key 'pluk' in the
dictionary __dict__ of the object whose 'name' is, in
turn, 'plik'...).

It makes no difference that you want to write your code
for whatname() in C++, by the way.  Your possibilities
to determine "names" starting from values are basically
the same as in Python (except in Python it will be
easier, but the substance doesn't really change).  So
you may as well prototype whatname() in Python, and,
once you've put it to the point you desire, either
distribute it in Python (and call it from C++ via the
API), or recode it in C++ if you prefer.


For example, let's start unambitiously: we'd be
content with knowing the key ('name') of any entry
in globals() ('global variable' -- not caring for
now about builtins or locals) whose corresponding
value satisfies the 'is' relationship with the
mystery-value.  This one is easy...:

def whatname(mystery, globs):
    for key,value in globs.items():
        if value is mystery: return key
    return None

But note that we are receiving 'globs' as a
dictionary -- each module has different 'globals',
so it wouldn't do to use *our own*... the caller
must supply the 'globals' dictionary in which
we are to search!

So, this also works for local variables -- just
call it with locals() as the second argument --
or ANY variable -- call it with vars().

But of course it won't know anything about
list-entries, entries in other dictionaries
(such as the __dict__'s of modules and other
objects), etc.  Is that OK anyway for your
specific needs...?


Are we on the right track to meet your desiderata?

Let us know, and we'll proceed with the thornier
issue of how we can avoid the called having to
pass us the specific dictionary in which we are
to search...


Alex






More information about the Python-list mailing list