FAQ
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

Search Discussions

  • Peter Otten at Dec 13, 2009 at 11:34 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()

    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
  • Chris Rebert at Dec 13, 2009 at 11:34 am

    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
  • Vsoler at Dec 13, 2009 at 6:23 pm

    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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedDec 13, '09 at 11:20a
activeDec 13, '09 at 6:23p
posts4
users3
websitepython.org

People

Translate

site design / logo © 2022 Grokbase