Testing if a global is defined in a module

Chris Rebert clp2 at rebertia.com
Mon Jul 4 17:03:32 EDT 2011


On Mon, Jul 4, 2011 at 11:11 AM, Tim Johnson <tim at johnsons-web.com> wrote:
> Using Python 2.6 on ubuntu 10.04.
> inspect module :
> I want to 'inspect' a module and get a list of all
> functions, classes and global variables in that module.
You meant "first defined in" that module.
<snip>
> Example, for a module name `mvcInstall', when a class
> name `Install' that is defined in the module
> is passed as an argument to inspect.getmodule, the
> values returned is something like
> "<module 'mvcInstall' from
> '/home/tim/prj/cgi/libraries/python/mvcInstall.py'>"
> Likewise for functions defined in the module.
>
> ** But ** when global variables such as strings, booleans,
> integers are passed as an argument to getmodule, the
> value returned is `None'.
>
> What else can I do here?

Look at the names in the module's import statements using the `ast`
module, and exclude those from the set of names defined in the module.
Won't work for `from foo import *`, but that's bad practice and should
be refactored anyway.

Cheers,
Chris
--
http://rebertia.com



More information about the Python-list mailing list