From: vsoler on
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
From: Peter Otten on
vsoler wrote:

> 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
From: Chris Rebert on
On Sun, Dec 13, 2009 at 3:20 AM, vsoler <vicente.soler(a)gmail.com> wrote:
> 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
--
http://blog.rebertia.com
From: vsoler on
On Dec 13, 12:34 pm, Chris Rebert <c...(a)rebertia.com> wrote:
> On Sun, Dec 13, 2009 at 3:20 AM, vsoler <vicente.so...(a)gmail.com> wrote:
> > 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
> --http://blog.rebertia.com

Thank you very much. I now know how to proceed