unable to read the __main__ namespace

Discussion in 'Python' started by vsoler, Dec 13, 2009.

  1. vsoler

    vsoler Guest

    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
    vsoler, Dec 13, 2009
    #1
    1. Advertising

  2. vsoler

    Peter Otten Guest

    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
    Peter Otten, Dec 13, 2009
    #2
    1. Advertising

  3. vsoler

    Chris Rebert Guest

    On Sun, Dec 13, 2009 at 3:20 AM, 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()


    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
    Chris Rebert, Dec 13, 2009
    #3
  4. vsoler

    vsoler Guest

    On Dec 13, 12:34 pm, Chris Rebert <> wrote:
    > On Sun, Dec 13, 2009 at 3:20 AM, 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()

    >
    > 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
    vsoler, Dec 13, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marc Shapiro
    Replies:
    2
    Views:
    640
    Marc Shapiro
    Dec 19, 2003
  2. Amy G
    Replies:
    4
    Views:
    365
    Amy G
    Feb 3, 2004
  3. Hakan Nilsson
    Replies:
    1
    Views:
    304
    Mirko Zeibig
    Feb 3, 2004
  4. Eli Stevens \(WG.c\)

    if (__name__ == '__main__'): main(sys.argv[1:])

    Eli Stevens \(WG.c\), Apr 20, 2004, in forum: Python
    Replies:
    1
    Views:
    874
    Peter Hansen
    Apr 20, 2004
  5. =?iso-8859-1?Q?Fran=E7ois?= Pinard

    Re: if (__name__ == '__main__'): main(sys.argv[1:])

    =?iso-8859-1?Q?Fran=E7ois?= Pinard, Apr 21, 2004, in forum: Python
    Replies:
    7
    Views:
    957
    =?iso-8859-1?Q?Fran=E7ois?= Pinard
    Apr 23, 2004
Loading...

Share This Page