[Tutor] what variables are in the memory

Daniel Yoo dyoo@hkn.EECS.Berkeley.EDU
Fri, 4 Aug 2000 12:11:12 -0700 (PDT)


On Fri, 4 Aug 2000, Zuohua Zhang wrote:

> How to find out what variable are in the memory?
> Is there a way to get help info. on functions? Sth like help funcname?

If you want to know what variables are available to you, try the
vars() command.  For example, here's a small interpreter session that
shows a somewhat typical example:


>>> x = 25
>>> y = "cathedral"
>>> z = list("bazaar")
>>> vars()
{'y': 'cathedral', '__doc__': None, 'z': ['b', 'a', 'z', 'a', 'a', 'r'],
'x': 25, '__name__': '__main__', '__builtins__': <module '__builtin__'
(built-in)>}


vars(), then, gives you a hash of local variable names.  To get all the
globals, you'd use globals().


To learn more about these functions, take a look at:

  http://www.python.org/doc/current/lib/built-in-funcs.html

and search under vars() or globals().



About interactive help and documentation: many functions and modules have
"documentation" strings.  They are named

  [variable or module].__doc__

and can be printed.  For example:

###
>>> import string
>>> print string.__doc__
Common string manipulations.

Public module variables:

whitespace -- a string containing all characters considered whitespace
lowercase -- a string containing all characters considered lowercase
letters
uppercase -- a string containing all characters considered uppercase
letters
letters -- a string containing all characters considered letters
digits -- a string containing all characters considered decimal digits
hexdigits -- a string containing all characters considered hexadecimal
digits
octdigits -- a string containing all characters considered octal digits
###


Not everything does though.  I think the Python designers are planning to
add a doc() function in the 2.0 series soon, which should make things
easier.

Also, you can always call dir(object) to find out some of the attributes
of an object; this comes in handy:

>>> f = open("foo.txt")
>>> dir(f)
['close', 'closed', 'fileno', 'flush', 'isatty', 'mode', 'name', 'read',
'readinto', 'readline', 'readlines', 'seek', 'softspace', 'tell',
'truncate', 'write', 'writelines']


Hope this is what you're looking for!