vars() and dir()

Jeff Shannon jeff at
Thu Aug 26 22:24:39 CEST 2004

Claire wrote:

>i use vars() or dir() to get variables. I use them in a def block, so
>i get variables of that block. How can i get variables from the
>namespace __main__ ?
>I use python in interactive mode and want to have a function to get a
>dictionnary for python types (int, ...) and another for my types (my
>classes). this function will be in my pythonstartup file.
>If i use python in interactive mode, is there a main module defined ?
>i don't know how to use namespace __main__ to get information on this
>"global namespace" like i can do with math or another module

There is also a globals() function which operates in a manner parallel 
to vars().  Note that vars(), without an argument, is equivalent to 
locals().  Also beware that, while these functions will return a 
dictionary containing the current global/local variables, modifying that 
dictionary may not modify actual variables.  (In general, you're best 
off using them as read-only.)

Jeff Shannon
Credit International

More information about the Python-list mailing list