Re: classes vs. modules vs. types

Jaap Vermeulen (jaap@sequent.com)
Mon, 2 May 94 21:06:04 GMT

In <MONTNARO.94May2133540@spyder.crd.ge.com> montnaro@spyder.crd.ge.com (Skip Montanaro) writes:

>I'm just getting started with Python and am wondering what distinctions, if
>any, there are between modules, classes, and built-in types. I'm interested

Modules: Single namespace, accessible through modulename.var.
A module also acts as the global namespace for any object
created in that module.
Classes: Multiple namespaces, search algorithm through base classes.
The search algorithm starts in the instance object, travels up
to the class object and then all base classes from left to
right as they were declared, depth first.
__builtin__: Namespace that is searched last, always.

In general the lookup algorithm for a variable is local namespace,
global namespace, __builtin__ namespace. Most code objects create a
local namespace when executed; the global namespace is the module the
code object was declared in.

What you call builtin types are probably builtin objects or modules
that have been hardcoded in C.

>in understanding how I might integrate C++ classes into Python. To add some
>concreteness to my problem, suppose I have a simple C++ class hierarchy:
>
> shape
> circle
> rectangle
> triangle
>
>Shape is abstract and defines getcolor() and setcolor() methods, and
>declares a single abstract virtual method, getarea(). Circle, rectangle, and
>triangle must define getarea().
>
>Would each class be treated as a module or would the four classes together
>be a single module? Are there any examples of classes written in C, or is
>that not possible? (Must classes be written in the scripting language?) If
>so, are classes written in C considered equivalent to built-in types?

You could go either way. It will be simpler to put everything in one
module, as all methods will share the same global namespace. However,
if you implement it pure OO, you would be using class variables anyway,
so it doesn't matter.

>I looked at the code in posixmodule.c and arraymodule.c. The former seems to
>provide few class concepts (e.g., no ability to create new objects at
>run-time), just a new namespace (posix) populated with a bunch of
>functions. The array module is much more complicated. Is the creating of a
>complex module like "array" documented somewhere?

You are looking at builtin modules, hardcoded in C. Better look at
cmd.py, bdb.py and pdb.py in the library.

Hope this helps,

-Jaap-

-- 
Jaap Vermeulen					+--------------------------+
						| Sequent Computer Systems |
	Internet : jaap@sequent.com		| Beaverton, Oregon	   |
	Uucp	 : ...uunet!sequent!jaap	+--------------------------+