How to get self reference from within a module?

Gary Herron gherron at digipen.edu
Mon Jan 22 18:46:21 EST 2007


Lavoie Érick wrote:
> Hi,
>
>  i would like to now how to get a self reference from within a 
> module.  The goal is to be able to generate a list of all declared 
> function within the module.
>
> Thanks,
>
> Erick
I think this is what you're asking for:

If you import a module, say

  import sys

then sys is an object you can refer to.  Like man objects in Python, you 
can do some ,so called, introspection.  For instance you can ask for the 
list of all the attributes (function, classes, and such) with the vars 
builtin

  print vars(sys)

That yields a dictionary of all objects in the module, from which you 
can access and iterate through the names (keys) and values.

If you want more capabilities, you should also look at the inspect module.

Gary Herron



-- 
Gary Herron, PhD.
Department of Computer Science
DigiPen Institute of Technology
(425) 895-4418





More information about the Python-list mailing list