printing all variables
Sheldon
shejo284 at gmail.com
Mon Jun 12 04:31:38 EDT 2006
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
More information about the Python-list
mailing list