How to obtain an instance's name at runtime?
Sep 15th, 2009 02:20
jick heir, johnny deep, Nathan Wallace, Hans Nowak, Snippet 101, Steven D. Majewski
> > Well, there sort of is a solution, but I don't think you're going
> > to like it. Incidentally, why are you trying to discover this? I'm
> > not a super-meister-coder, but I can't imagine a situation where
> > knowing this would be useful.
> Ok, I knew I was going to look like a dumb camel right after
> posting my question... Nevertheless there is a useful case of
> knowing such an instance's name, at least I believe I found
> one. Have a look at the following page, spot the question mark
> and you'll know what I mean:
> But, PLEASE, don't start to ask me anything about the project
> itself!! It's all in a very pre-pre-pre-alpha phase...
> There might be some workaround solution in oder to get that
> name I want, as in some of the replied in this thread, but
> I'll evaluate that later...
It's easy to get *A* name --
Classes and functions have cannonical names in Python because Python
distinguishes between definition and assignment for classes and
functions. They also have a cannonical namespace -- for classes,
their __module__ attribute, for Python functions, their func_globals
attribute (which is the __dict__ of the __module__ they were defined
in.) That's why their names are "sticky" .
Classes and functions can be aliased and assigned to other names,
but for debugging, tracebacks and printing, you want that cannonical
There are other types of objects that could benefit from "sticky"
names: constants and enumns for example. But Python doesn't
have a different syntax to indicate a definition separate from
any other assignment.
You can get a name by searching for the thing in all of the namespaces:
import sys # added by PSST
for mod in sys.modules.values():
for name,val in mod.__dict__.items():
if val is thing : return name
print findname( sys.stdin )
print findname( findname )
print findname( lambda x: x+1 ) # anonymous function doesn't have a name
# returns None
xxx = sys.stdin # alias sys.stdin to another name
print findname( sys.stdin ) # finds *a* name (whichever is first)
# Try the __name__ attribute first before searching...
def nameof( THING ):
return getattr( THING, '__name__', None ) or findname( THING )
print nameof( lambda x: x+1 )
mod = sys
print nameof( mod )
 You could do a smarter seach of the name space, but I'm not sure
if that would be any better for your application.
 You could also append the module name to the name and return
"sys.stdin" rather than "stdin" above.
 Might there be cases where you want the '__repr__' rather than
the name ? i.e. "<open file '<stdin>', mode 'r' at 5a28500>" rather