[Python-Dev] Tutorial: Brief Introduction to the Standard Libary

Alex Martelli aleax at aleax.it
Thu Nov 27 05:13:05 EST 2003

On Wednesday 26 November 2003 09:56 pm, Raymond Hettinger wrote:

> I'm adding section to the tutorial with a brief sampling of library
> offerings and some short examples of how to use them.

Great idea.

>    copy, glob, shelve, pickle, os, re, math/cmath, urllib, smtplib
> I'm interested to know what your experiences have been with teaching
> python.  Which modules are necessary to start doing real work (like

I would add:

sys -- "real programs" want to access their command-line arguments
(sys.argv), want to terminate (sys.exit), want to write to sys.stderr.

fileinput -- users are VERY likely to want to "rewrite textfiles in-place"
(as well as wanting to read a bunch of textfiles) and fileinput is just
the ticket for that.  Users coming from perl particularly need fileinput
desperately as it affords close translation of the "while(<>)" idiom.

cStringIO -- I've noticed most newbies find it more natural to "write to
a cStringIO.StringIO pseudofile as they go" then getvalue, rather than
append'ing to a list of strings then ''.join .

time, datetime, calendar -- many real programs want to deal with
dates and times

array -- many newbies try to use lists to do things that are perfect
for array.array's

> pickle and os), which are most easily grasped (like glob or random),
> which have impressive examples only a few lines long (i.e. urllib), and

I think zipfile and gzip are easily grasped AND impressive for people
who've ever needed to read/write compressed files in other languages.
xmlrpclib and SimpleXMLRPCServer are also eye-poppers (and despite
their names you don't need to get into XML at all to show them off:-).
CGIHTTPServer, while of course not all that suitable for "real programs",
has also contributed more than its share in making instant converts to
Python, in my experience -- "instant gratification".

> I'm especially interested in thoughts on whether shelve should be
> included.  When I first started out, I was very impressed with shelves
> because they were the simplest way to add a form of persistence and
> because they could be dropped in place of a dictionary in scripts that
> were already built.  Also, it was trivially easy to learn based on
> existing knowledge of dictionaries.  OTOH, that existing knowledge is
> what makes the pitfalls so surprising.

Hmmm, yes, but, with writeback=True, you do work around the most
surprising pitfalls (at a price in performance, of course).  I dunno -- with
so many other impressive modules to show off, maybe shelve might
be avoided.

> - threading/Queue (because without direction people grab thread and
> mutexes)

True, they do.  But I don't know if the tutorial is the right time to 
indoctrinate people about proper Python threading architectures.

> - timeit (because it answers most performance questions in a jiffy)
> - unittest (because TDD folks like myself live by it)

Absolute agreement here.  And doctest is SO easy to use, that for
the limited space of the tutorial it might also be quite appropriate -- it
also encourages abundant use of docstrings, a neat thing in itself.


More information about the Python-Dev mailing list