Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > printing all variables

Reply
Thread Tools

printing all variables

 
 
Sheldon
Guest
Posts: n/a
 
      06-12-2006
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

 
Reply With Quote
 
 
 
 
Duncan Booth
Guest
Posts: n/a
 
      06-12-2006
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': []}
>>>

 
Reply With Quote
 
 
 
 
Sheldon
Guest
Posts: n/a
 
      06-12-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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 2 11-27-2010 12:12 PM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-21-2007 05:52 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-21-2007 05:50 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-21-2007 05:28 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-18-2007 10:11 AM



Advertisments