469,927 Members | 1,464 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,927 developers. It's quick & easy.

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

Jun 12 '06 #1
2 1857
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': []}

Jun 12 '06 #2

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

Jun 12 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Jack Russell | last post: by
10 posts views Thread by Jeff B. | last post: by
6 posts views Thread by Bill | last post: by
reply views Thread by Jason Hunt | last post: by
2 posts views Thread by =?Utf-8?B?TUFUVA==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.