1. Unless you are doing something very special (like a debugger), needing
this seems like a bad design choice. Just my shnekel.
2. Although you can (in some cases for some value of some) define it,
talking about an "official address" of an object, seems extremely
3. To your actual question: you can use the gc module to get the referrers
to a specific object. By traversing this tree structure, you can probably
extract the information you want. Furthermore, for many objects, such as
instances and functions, finding the definition point is not that hard
(using the inspect module). In any case it seems to me that what you seek
do seek to do will be very hard to accomplish correctly, require some "deep
magic", and IMHO, yield low returns.
On Fri, Sep 10, 2010 at 5:46 PM, cool-RR wrote:
> Hello everyone!
> I have a question:
> Is there a Python module for handling Python object addresses?
> When I say "object address", I mean the string that you type in Python to
> access an object. For example `'life.State.step'`. Most of the time, all
> objects before the last dot will be packages/modules, but in some cases
> can be classes or other things.
> (Another note: When I say "object", I mostly mean classes, modules,
> functions, methods, stuff like these, which usually do not change their
> In my Python project I often have the need to play around with object
> addresses. Some tasks that I have to do:
> 1. Given an object, get its address.
> 2. Given an address, get the object, importing any needed modules on
> the way.
> 3. Shorten an object's address by getting rid of redundant
> modules. (For example, `'life.life.State.step'` may be the official
> of an object, but if `'life.State.step'` points at the same object,
> to use it instead because it's shorter.)
> 4. Shorten an object's address by "rooting" a specified module. (For
> example,`'garlicsim_lib.simpacks.prisoner.prisoner.State.step'` may be
> official address of an object, but I assume that the user knows where
> the prisoner package is, so I'd want to use
> as the address.)
> Is there a module/framework that handles things like that? I wrote a few
> utility modules to do these things, but if someone has already written a
> more mature module that does this, I'd prefer to use that.
> One note: Please, don't try to show me a quick implementation of these
> things. It's more complicated than it seems, there are plenty of gotchas,
> and any quickly-written code will probably fail for many important cases.
> These kind of tasks call for battle-tested code, and I just want to know
> such code exists or whether I will have to write it myself.
> Python-il mailing list
http://plnnr.com/ - automatic trip planning
-- insert signature here ----