unable to read the __main__ namespace

V

vsoler

I'm learning Python, and I am very fond of it.

Using Python 2.6

I am able to list all the names in a class namespace:

class abc: pass
abc.a1=7
abc.a2='Text'

print abc.__dict__.keys()

a) However, I do not know how to read the __main__ namespace

print __main__.__dict__.keys() # Just does not work

b) How do i read an imported module namespace?

c) How could I possibly list the names and contents of the namespace
of my python session?

Thank you for your help.

Vicente Soler
 
P

Peter Otten

vsoler said:
I'm learning Python, and I am very fond of it.

Using Python 2.6

I am able to list all the names in a class namespace:

class abc: pass
abc.a1=7
abc.a2='Text'

print abc.__dict__.keys()

a) However, I do not know how to read the __main__ namespace

print __main__.__dict__.keys() # Just does not work

It should work. Did you forget to

import __main__

before trying?
b) How do i read an imported module namespace?

c) How could I possibly list the names and contents of the namespace
of my python session?

The answer to all your questions is dir() for names and vars() for the
complete namespace dictionary:
x = 42
dir() ['__builtins__', '__doc__', '__name__', '__package__', 'x']
import os
dir(os)[:5] ['EX_CANTCREAT', 'EX_CONFIG', 'EX_DATAERR', 'EX_IOERR', 'EX_NOHOST']
vars()
{'__builtins__': <module '__builtin__' (built-in)>, '__package__': None,
'x': 42, '__name__': '__main__', 'os': <module 'os' from
'/usr/lib/python2.6/os.pyc'>, '__doc__': None}

Peter
 
C

Chris Rebert

I'm learning Python, and I am very fond of it.

Using Python 2.6

I am able to list all the names in a class namespace:

class abc: pass
abc.a1=7
abc.a2='Text'

print abc.__dict__.keys()

That is more simply written as:

print dir(abc)
a) However, I do not know how to read the __main__ namespace

print __main__.__dict__.keys()    # Just does not work

__main__ is not used or set and has no special meaning to the Python
interpreter. It's true that __name__ == "__main__" in the body of the
main module, but you can't actually access it by that name.
b) How do i read an imported module namespace?

import module
print dir(module)
c) How could I possibly list the names and contents of the namespace
of my python session?

print dir()

I'd advise reading the docs on it:
http://docs.python.org/library/functions.html#dir

Cheers,
Chris
 
V

vsoler

That is more simply written as:

print dir(abc)



__main__ is not used or set and has no special meaning to the Python
interpreter. It's true that __name__ == "__main__" in the body of the
main module, but you can't actually access it by that name.


import module
print dir(module)


print dir()

I'd advise reading the docs on it:http://docs.python.org/library/functions.html#dir

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

Thank you very much. I now know how to proceed
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top