printing all variables

Discussion in 'Python' started by Sheldon, Jun 12, 2006.

  1. Sheldon

    Sheldon Guest

    Good day,

    I would like to know if there is a way to print all the variables set
    in a python program with having to write
    "print variable" on all?

    sincerely,
    Sheldon
     
    Sheldon, Jun 12, 2006
    #1
    1. Advertising

  2. Sheldon

    Duncan Booth Guest

    Sheldon wrote:

    > Good day,
    >
    > I would like to know if there is a way to print all the variables set
    > in a python program with having to write
    > "print variable" on all?
    >

    Not all the variables in a program (that would be rather more than you
    want), but you can print all the variables in a specific namespace easily
    enough:

    >>> from pprint import pprint
    >>> def f(x):

    pprint(locals())


    >>> f(2)

    {'x': 2}
    >>> pprint(globals())

    {'__builtins__': <module '__builtin__' (built-in)>,
    '__doc__': None,
    '__name__': '__main__',
    'f': <function f at 0x00B45B30>,
    'pprint': <function pprint at 0x00B45BB0>}
    >>> class C:

    classvar = []
    def __init__(self, n):
    self.n = n


    >>> c = C(3)
    >>> pprint(vars(c))

    {'n': 3}
    >>> pprint(vars(C))

    {'__doc__': None,
    '__init__': <function __init__ at 0x00B4A070>,
    '__module__': '__main__',
    'classvar': []}
    >>>
     
    Duncan Booth, Jun 12, 2006
    #2
    1. Advertising

  3. Sheldon

    Sheldon Guest

    Duncan Booth skrev:

    > Sheldon wrote:
    >
    > > Good day,
    > >
    > > I would like to know if there is a way to print all the variables set
    > > in a python program with having to write
    > > "print variable" on all?
    > >

    > Not all the variables in a program (that would be rather more than you
    > want), but you can print all the variables in a specific namespace easily
    > enough:
    >
    > >>> from pprint import pprint
    > >>> def f(x):

    > pprint(locals())
    >
    >
    > >>> f(2)

    > {'x': 2}
    > >>> pprint(globals())

    > {'__builtins__': <module '__builtin__' (built-in)>,
    > '__doc__': None,
    > '__name__': '__main__',
    > 'f': <function f at 0x00B45B30>,
    > 'pprint': <function pprint at 0x00B45BB0>}
    > >>> class C:

    > classvar = []
    > def __init__(self, n):
    > self.n = n
    >
    >
    > >>> c = C(3)
    > >>> pprint(vars(c))

    > {'n': 3}
    > >>> pprint(vars(C))

    > {'__doc__': None,
    > '__init__': <function __init__ at 0x00B4A070>,
    > '__module__': '__main__',
    > 'classvar': []}
    > >>>


    Thanks Duncan! This really helps!

    /Sheldon
     
    Sheldon, Jun 12, 2006
    #3
    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. Jon B
    Replies:
    7
    Views:
    7,554
    =?Utf-8?B?SmFzb25DaG9p?=
    Jan 30, 2006
  2. JustSomeGuy
    Replies:
    13
    Views:
    519
    msalters
    Dec 9, 2004
  3. printing all variables in union

    , Jul 25, 2005, in forum: C Programming
    Replies:
    14
    Views:
    480
    Steven Kobes
    Jul 26, 2005
  4. Replies:
    9
    Views:
    984
  5. iffy agbim
    Replies:
    1
    Views:
    114
    Mark Andrews
    May 24, 2004
Loading...

Share This Page