Print variable values by their names

Peter Otten __peter__ at web.de
Mon Oct 18 19:34:59 CEST 2004


Michael Krasnyk wrote:

> How can I print variable values by their names?
> For example:
> 
> import sys
> for str in dir(sys): print str
> 
> Will be printed only variable names, but I need values from list of
> variable names.

vars(module) gives you the module's global dictionary:

>>> import sys
>>> for name, value in vars(sys).items():
...     print name, "=", repr(value)
...
setrecursionlimit = <built-in function setrecursionlimit>
getfilesystemencoding = <built-in function getfilesystemencoding>
stdout = <open file '<stdout>', mode 'w' at 0x40335060>
[snip]

Peter




More information about the Python-list mailing list