Import a module without executing it?
finite.automaton at gmail.com
Wed Dec 8 01:19:21 CET 2004
> The real question, I suppose, is "what is a good technique to find
> modules and classes implement or refer to particular names"
I think your best bet is still to import the module and introspect it.
It will execute some code, but (by convention) simply importing a
module doesn't usually unleash any spectacular computation. Scripts
meant to be executed from the command line will almost always have an
(if __name__ == '__main__' ) clause to prevent just this.
A search of this newsgroup for "duck" or "duck typing" will probably
turn up more info on how people approximate the "X implements Y"
relationship in Python.
On the other hand, if you're just trying to figure out what a black-box
object does from the interpreter's command line, you can use help(),
dir() or pydoc amongst others. "import pydoc; pydoc.gui()"
There are some functions that will be handy for determining properties
of an unknown object:
callable, eval, hasattr, isinstance, type, super
And most objects will have a few magic attributes-
>> def qux(x, y=3): pass
['__call__', '__class__', '__delattr__', '__dict__', '__doc__',
'__get__', '__getattribute__', '__hash__', '__init__', '__module__',
'__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
'__setattr__', '__str__', 'func_closure', 'func_code', 'func_defaults',
'func_dict', 'func_doc', 'func_globals', 'func_name']
More information about the Python-list