[Tutor] information hiding = interface & documntation hiding

Daniel Yoo dyoo@hkn.eecs.berkeley.edu
Tue, 26 Dec 2000 18:34:37 -0800 (PST)


On Mon, 25 Dec 2000, kevin parks wrote:

> information hiding is supposed to olny hide the implementation, not
> the interface! But unfortunately, the interfac for a piece of code is
> mysterious to me. There are tons of modules, packages, proceedures,

Each module has documentation with it.  For example, if we wanted to know
more about the "string" module, we'd look at the Library Reference at:

    http://python.org/doc/current/lib/lib.html

If you look through, you'll find a link that says "String Services", and
under that, the string module documentation.

    http://python.org/doc/current/lib/module-string.html

Also, the interpreter is wonderful when you want to figure out how a
module works.  Let's show a sample interpreter session:

###
>>> import string
>>> dir(string)
['__builtins__', '__doc__', '__file__', '__name__', '_idmap', '_idmapL',
'_lower
', '_re', '_safe_env', '_swapcase', '_upper', 'atof', 'atof_error',
'atoi', 'ato
i_error', 'atol', 'atol_error', 'capitalize', 'capwords', 'center',
'count', 'di
gits', 'expandtabs', 'find', 'hexdigits', 'index', 'index_error', 'join',
'joinf
ields', 'letters', 'ljust', 'lower', 'lowercase', 'lstrip', 'maketrans',
'octdig
its', 'replace', 'rfind', 'rindex', 'rjust', 'rstrip', 'split',
'splitfields', '
strip', 'swapcase', 'translate', 'upper', 'uppercase', 'whitespace',
'zfill']
>>> # let's figure out how to use swapcase 
... 
>>> print string.swapcase.__doc__
swapcase(s) -> string

Return a copy of the string s with upper case characters
converted to lowercase and vice versa.
>>> string.swapcase("This is a test.")
'tHIS IS A TEST.'
###


The big thing to notice is that, for properly documented functions, you
can print out its "docstring"  --- a small summary that describes basic
use and the argument types.  Every function usually has a .__doc__
attribute that you can print out.


> objects, classes, and function that come with the python distribution
> and also to be fond on the net. The problem is knowing what input they
> expect and what the output is. Only the people who program them seem
> to know this and there is often little or no documentation to b found.

Learning the features of any standard library comes with experience and
practice.  You may want to talk to other Python programmers to find out
what modules they find useful.  For example, I usually use "string", "re",
and "math", because I focus on doing string manipulation and playing with
math.  When I want to do stuff like downloading URL's, I use "urllib".

However, your focus may be different --- a good thing to do is to browse
through the titles on the Library Reference, and see what catches your
eye.


> Additionally there are often dependencies to be known about and often
> not to be found.  I would really like help with this. This whole OOP
> thing is such a farce sometimes. The whole idea is that you are
> supposed to be able to mak use of code thatyo didn't (or in my case
> couldn't) implement yourself. I am not an algorythms guy and i don't
> want to reinvent the wheel. i would like to use all the code inthe
> python image, but getting to know what is in the image and how to use
> it is so hard. What tools does python have for this? If any? How cani

Well, you have us.  *grin* Also, the documentation isn't as bad as you may
think --- again, take a look at the Library Reference.  If there's
anything that seems weird, ask us; there's bound to be someone else who's
played around with the same module.


> know if my code names are unque orif my funcs are overiding somthing
> that aleady exsits. In TCL one just types a nam in the the shll. if
> there is a proc of that name it comes up and the interface is often
> tere too. For example, i saw there was a markov.py file in the

That should work.  Try it from the interpreter --- you should be able to
see what names are defined for you.  More directly, you can use the
dir() function to see what variables and functions are available.

About markov.py --- I have no idea; I haven't looked into it yet.  It
doesn't appear to be part of the standard library, so your best bet is to
look in markov.py itself.

As a note, you do NOT have to memorize the whole standard library.  
There's not going to be a test on it.  *grin* Just concentrate on the
stuff that you want to use, and you should do ok.