Who am I: can a class instance determine its own name?

Alex Martelli aleaxit at yahoo.com
Fri Mar 9 06:56:29 EST 2001


"Tim CHURCHES" <TCHUR at doh.health.nsw.gov.au> wrote in message
news:mailman.984034150.9635.python-list at python.org...
> This is probably an elementary question and the answer is probably writ
large in
> multiple places in the Python documentation, but...

> ...can an instance of a class determine the name of the variable to which
it is
> assigned? For example:

As many respondents have already stated, the answer has to be
No; any object, at any given moment, may be referred-to by any
number N of references [[an aside: in the current version of
CPython, it must be the case that N>0, or the object would be
destroyed; in Jython, in Python .NET, and possibly in future
versions of CPython, this guarantee does not hold]], including
maybe both ones that are named, and other ones that are not
named (but, rather, indexed).

For example,

    adict = {}
    adict['!}@}!'] = []
    adict['!}@}!'].append(MyClass())

now, what is "the name of the variable" you would wish to
determine for this instance, which can at this time be
referred to only as adict['!}@}!'][0] but 'at a drop of a
hat' might also be seen through other references, not be
seen through this one any more, etc, etc...?


Asking an object "under what names [and/or unnamed refs]
are you known" is akin to asking an individual "on what
web sites (URLs) are you mentioned".  The individual most
likely has no idea: he or she is typically not informed
each and every time a reference to him/her is added to
some URL or removed from it, nor does the existence of
such reference have any effect directly observable on/by
him/her.

If you wanted to find all such URLs, you would have to
grep through ALL URLs of the web, and, for each one,
determine if it refers to the individual of interest --
this is basically what search engines such as Google do.

Similarly, if you want to find all references to some
Python object, you will have to grep through all the
objects currently existing, recursively determining all
the references held, and see which of these references
are the ones of your interest (use the 'is' operator
for this purpose).  It makes no real sense to delegate
this job to each individual/object you might be looking
for -- do it in a global function (or utility class) so
you can reuse it for any object, regardless of type.


Supposing you're not interested in unnamed (indexed)
references, local variables, or attributes of instance
objects, but only in attributes of modules (module-level
'global variables'), for example, here's a start...:

def namesOf(anobject):
    result = []
    import sys
    for modname,modobj in sys.modules.items():
        for varname,varobj in modobj.__dict__.items():
            if varobj is anobject:
                result.append("%s.%s" % (
                    modname, varname))
    return result

import sys, re, string

sys.blab = re.blob = string.blub = "fakaoplod"

names = namesOf(sys.blab)
for name in names:
    print name


Alex






More information about the Python-list mailing list