By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,222 Members | 1,436 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,222 IT Pros & Developers. It's quick & easy.

vars() and dir()

P: n/a
hi,
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

Thanks

Claire
Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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__ ?
vars(__main__) or dir(__main__) works if you import __main__ first.
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 ?
Yes, but you always have to import stuff before you can use it. Do
import __main__ and it gives you an alias for the current namespace,
but the result is the same as using plain vars() or dir() there.
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


No difference, just import first.

If this doesn't help, please try explaining further what you are
trying to accomplish. I don't think it's entirely clear from what you
have said above.

-Peter
Jul 18 '05 #2

P: n/a
Claire wrote:
hi,
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
Technician/Programmer
Credit International

Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.