Is there a Python module for handling Python object addresses?
Posted
by
cool-RR
on Stack Overflow
See other posts from Stack Overflow
or by cool-RR
Published on 2010-09-10T11:15:18Z
Indexed on
2010/12/23
22:54 UTC
Read the original article
Hit count: 166
(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 objects.)
In my Python project I often have the need to play around with object addresses. Some tasks that I have to do:
- Given an object, get its address.
- Given an address, get the object, importing any needed modules on the way.
- 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.) - 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 theprisoner
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 quick-n-dirty code will probably fail for many important cases. These kind of tasks call for battle-tested code.
UPDATE: When I say "object", I mostly mean classes, modules, functions, methods, stuff like these. Sorry for not making this clear before.
© Stack Overflow or respective owner