printing all variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sheldon

    #1

    printing all variables

    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

  • Duncan Booth

    #2
    Re: printing all variables

    Sheldon wrote:
    [color=blue]
    > 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?
    >[/color]
    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:
    [color=blue][color=green][color=darkred]
    >>> from pprint import pprint
    >>> def f(x):[/color][/color][/color]
    pprint(locals() )

    [color=blue][color=green][color=darkred]
    >>> f(2)[/color][/color][/color]
    {'x': 2}[color=blue][color=green][color=darkred]
    >>> pprint(globals( ))[/color][/color][/color]
    {'__builtins__' : <module '__builtin__' (built-in)>,
    '__doc__': None,
    '__name__': '__main__',
    'f': <function f at 0x00B45B30>,
    'pprint': <function pprint at 0x00B45BB0>}[color=blue][color=green][color=darkred]
    >>> class C:[/color][/color][/color]
    classvar = []
    def __init__(self, n):
    self.n = n

    [color=blue][color=green][color=darkred]
    >>> c = C(3)
    >>> pprint(vars(c))[/color][/color][/color]
    {'n': 3}[color=blue][color=green][color=darkred]
    >>> pprint(vars(C))[/color][/color][/color]
    {'__doc__': None,
    '__init__': <function __init__ at 0x00B4A070>,
    '__module__': '__main__',
    'classvar': []}[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    Comment

    • Sheldon

      #3
      Re: printing all variables


      Duncan Booth skrev:
      [color=blue]
      > Sheldon wrote:
      >[color=green]
      > > 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?
      > >[/color]
      > 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:
      >[color=green][color=darkred]
      > >>> from pprint import pprint
      > >>> def f(x):[/color][/color]
      > pprint(locals() )
      >
      >[color=green][color=darkred]
      > >>> f(2)[/color][/color]
      > {'x': 2}[color=green][color=darkred]
      > >>> pprint(globals( ))[/color][/color]
      > {'__builtins__' : <module '__builtin__' (built-in)>,
      > '__doc__': None,
      > '__name__': '__main__',
      > 'f': <function f at 0x00B45B30>,
      > 'pprint': <function pprint at 0x00B45BB0>}[color=green][color=darkred]
      > >>> class C:[/color][/color]
      > classvar = []
      > def __init__(self, n):
      > self.n = n
      >
      >[color=green][color=darkred]
      > >>> c = C(3)
      > >>> pprint(vars(c))[/color][/color]
      > {'n': 3}[color=green][color=darkred]
      > >>> pprint(vars(C))[/color][/color]
      > {'__doc__': None,
      > '__init__': <function __init__ at 0x00B4A070>,
      > '__module__': '__main__',
      > 'classvar': []}[color=green][color=darkred]
      > >>>[/color][/color][/color]

      Thanks Duncan! This really helps!

      /Sheldon

      Comment

      Working...