Imri Goldberg | 10 Sep 20:23 2010
Picon

Re: Is there a Python module for handling Python object addresses?

Several points:
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 non-pythonic.
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 to do seek to do will be very hard to accomplish correctly, require some "deep magic", and IMHO, yield low returns.

Cheers,
Imri

On Fri, Sep 10, 2010 at 5:46 PM, cool-RR <cool-rr <at> cool-rr.com> 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 the objects before the last dot will be packages/modules, but in some cases they 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 address.)

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 intermediate modules. (For example, `'life.life.State.step'` may be the official address of an object, but if `'life.State.step'` points at the same object, I'd want 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 the official address of an object, but I assume that the user knows where the prisoner package is, so I'd want to use `'prisoner.prisoner.State.step'` 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 if such code exists or whether I will have to write it myself.


Thanks,
Ram.

_______________________________________________
Python-il mailing list
Python-il <at> hamakor.org.il
http://hamakor.org.il/cgi-bin/mailman/listinfo/python-il




--
Imri Goldberg
--------------------------------------
http://plnnr.com/ - automatic trip planning
http://www.algorithm.co.il/blogs/
--------------------------------------
-- insert signature here ----
_______________________________________________
Python-il mailing list
Python-il <at> hamakor.org.il
http://hamakor.org.il/cgi-bin/mailman/listinfo/python-il

Gmane