[Tutor] what variables are in the memory

Wesley J. Chun wesc@alpha.ece.ucsb.edu
Fri, 4 Aug 2000 12:29:32 -0700 (PDT)

    > From: Zuohua Zhang <zuzhang@sarnoff.com>
    > Date: Fri, 04 Aug 2000 14:47:48 -0400
    > How to find out what variable are in the memory?
    > Is there a way to get help info. on functions? Sth like help funcname?


ni hao!  zhi ge wenti hen hao!  let's take a look at both your questions:

1.  to find out what variables are currently in memory, just use the
    dir() built-in function:

>>> foo = 'xyz'
>>> bar = 42
>>> dir()
['__builtins__', '__doc__', '__name__', 'bar', 'foo']

notice that you get *your* variable names as well as the default
special variables __builtins__, __doc__, and __name__.

you can also get a dictionary of both the variables' names *and* values,
you can use globals() (or locals() from inside a function):

>>> globals()
{'__doc__': None, 'bar': 42, 'foo': 'xyz', '__name__': '__main__', '__builtins__': <module '__builtin__' (built-in)>}

there is another built-in function called vars() which without argu-
ments works just like locals() (and in the global namespace like
globals()), but it can also give information regarding modules which
have been imported or class instances, i.e. vars(string).  neither
locals() nor globals() take arguments.

dir() can also take a module or class instance and just give you the
list of names (as vars(), locals(), and globals() return dictionaries).

2.  yes, you can get help information for functions... with a syntax
    very similar to "help funcname".  you will notice above that there is
    a built-in __doc__ variable.  that is the name of the documentation
    variable for modules (.py files), functions, and classes.  Here is an
    example with the string.find() function:
>>> import string
>>> print string.find.__doc__
find(s, sub [,start [,end]]) -> in

    Return the lowest index in s where substring sub is found,
    such that sub is contained within s[start,end].  Optional
    arguments start and end are interpreted as in slice notation.

    Return -1 on failure.

- - - - - -

    Now you have to remember that not every internal function has been
    documented yet.  As a programmer, you are responsible for the
    documentation of your own code (modules, functions, and classes).
    The first unassigned string in such pieces code will be put in the
    __doc__ variable:

>>> def mult(x, y=3.14):
...     '''mult(x,y=3.14) -> float
... mult() takes a pair of numbers (y defaults to 3.14 if
... only one argument passed in) and returns their product.'''
...     return x * y
>>> mult(4)
>>> mult(4.1)
>>> mult(4., 2.)
>>> mult(4, 2)
>>> print mult.__doc__
mult(x,y=3.14) -> float
mult() takes a pair of numbers (y defaults to 3.14 if
only one argument passed in) and returns their product.

hope this helps!!


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

"Core Python Programming", Prentice Hall PTR, TBP Summer/Fall 2000

Python Books:   http://www.softpro.com/languages-python.html

wesley.j.chun :: wesc@alpha.ece.ucsb.edu
cyberweb.consulting :: silicon.valley, ca