[Tutor] omit some keys from dict

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Sun, 10 Feb 2002 13:59:20 -0800 (PST)

On Sun, 10 Feb 2002, Erik Price wrote:

> > modules written in pure python move forward from 1.x just fine 9 times 
> > out of
> > 10.  The reverse is 1 out of 10 when moving back to 1.x.
> >
> > The real problem are the modules written in C.
> I (mistakenly) thought that modules generally -were- pure python.

Not to say that they aren't --- many of the standard library modules are
in Python, just as we'd expect.  For example, the StringIO module is pure
Python, and the source code is available in a StringIO.py file in the
library.  It's useful to know that we can easily read most of the library
modules to see how they're constructed.

But we can also write modules in C if we put in the effort!  There's a
module called 'CStringIO' that does the same job as StringIO.  CStringIO
is written in C, and since readability isn't often a main goal in C,
CStringIO can implement a few unprintable optimizations that aren't easily
available in native Python.  *grin*

As long as we follow a few "ground rules", we can write modules in C for
fun and profit.  These ground rules are explained in the "Extending and
Embedding" documentation:


The problem about these extension modules is that the C API evolves with
Python, and this likely breaks compatibility.  A recompilation of an
extension module usually fixes things, but, of course, this requires that
we have a C compiler handy.

> I didn't realize that you could write a module in C and import it into
> Python.

Yes, this is what makes "extendable" languages like Python very cool ---
we can extend Python reliably through a module interface that makes
external code look indistinguishable from native Python code.

If you're interested in this sort of stuff, take a look at SWIG:


Good luck!