[Tutor] introspection

Steven D'Aprano steve at pearwood.info
Mon Apr 20 19:15:17 CEST 2015


On Mon, Apr 20, 2015 at 08:24:42AM -0700, Alex Kleider wrote:
> Does python provide the introspective ability to retrieve the name to 
> which an object is bound?

Short answer: no.

Slightly longer answer: it *may* be possible if you dig deep into the 
debugger API that you might find something which does what you want. I'm 
not saying that it will, only that if it exists, the debugger seems like 
the most likely place.

But... probably not.

In any case, even if you find something that will do this, it will be 
implementation-specific (e.g. Jython only, or IronPython only, or 
CPython only, but not all three) and probably rather dubious. After all, 
any object may have zero, one or more names, in completely different 
scopes. The whole idea of "what name does this object have" is rather 
dubious. The best one can do is say "in this namespace (locals), does 
this object have any names?".


-- 
Steve


More information about the Tutor mailing list