473,473 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

vars() and dir()

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
2 3574
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: james | last post by:
I am new to php and need some help getting the session variables into include files. (after-thought, Sorry for the drawn out post but I really, really need help....;) Here's what I'm doing.. ...
13
by: David Rysdam | last post by:
Getting no answer yesterday, I've done some investigation and I obviously don't understand how python namespaces work. Here's a test program: #!/usr/bin/python b = 2 def sumWithGlobal(a):...
0
by: Peter Steele | last post by:
I am creating a Visual Studio deployment project for the first time. It seems to be all straightforward except for one thing--how do I define environment vars? I suppose I could add registry...
3
by: Jon | last post by:
I have a couple of tables I want to load into a dataset and keep around pretty much forever, although they will need to be refreshed every so often. I can either put the dataset into an...
5
by: dannycolligan | last post by:
So I just got bitten by the "don't use a mutable object as an optional argument" gotcha. I now realize that for this function: .... y.append(x) .... print y .... y is initialized...
19
RMWChaos
by: RMWChaos | last post by:
Previously, I had used independent JSON lists in my code, where the lists were part of separate scripts. Because this method did not support reuse of a script without modification, I decided to...
6
by: goodguyjam | last post by:
Hi all, I'm having trouble with mysql. I've just finished my php coding for HTTP authentication and with some help am now getting a login window pop up whenever I click on a link on my website...
1
by: BrendanC | last post by:
I'm trying to understand reflection/introspection in Python. How can I identify the the type of attribute (e.g. instance var) in a class? The following returns all the class attributes (methods and...
5
by: Ross | last post by:
Forgive my newbieness - I want to refer to some variables and indirectly alter them. Not sure if this is as easy in Python as it is in C. Say I have three vars: oats, corn, barley I add them...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.