Re: classes vs. modules vs. types

Steven D. Majewski (sdm7g@elvis.med.virginia.edu)
Mon, 2 May 1994 15:48:25 -0400

Modules are really just a namespace full of other objects
( functions, variables, objects, classes ) and some initialization
code. They can be written in C or Python. If they are in C, then
typically the initialization code mostly just "registers" the
functions written in C to the Python interpreter. If they are
in Python, then the whole module is actually the initialization
code - it gets executed on the first import, and as a side effect,
it defines functions and populates it's namespace with created
objects.

Objects in Python are not necessarily class instances. Objects
can have methods, and other attributes, which is what makes the
word "object" applicable, but unless they are Classes or instances,
they are not part of an inheritance sequence.

Classes are objects with methods and variables that can create
instance objects and can inherit methods and variables from other
classes or can be inherited.

The sources for most builtin-C objects are in Objects/*object.c -
there is a simple demo template in xxobject.c .

The sources for most builtin-C modules are in Modules/*module.c
and there is a simple xxmodule.c demo.

But modules can contain objects - the main difference in organization
is that modules are importable, and thus a bit more optional than
objects. Thus a module like arraymodule, which contains one visible
function that creates an array object.

Class instance creation in Python looks syntatically like function
invocation, so:

x = array( 'f', [1.0, 2.0, 3.0] )

looks similar to:

class Array:
def __init__( self, code, *values ):
...

x = Array( 'f', [1.0, 2.0, 3.0] )

But 'array' is actually a function, while 'Array' is a class.

I don't believe that anyone has yet ever coded a (inheritable) Class in C
with it's methods coded in C, but I think that this should be possible.
( And you might just want to do this if, for example, you initially
coded your class definition in Python, and later wanted to rewrite it
for performance reasons, but you needed to keep it as a Class - not a
builtin-object, because other Classes might inherit from it. - although,
typically, we have used Class wrappers around builtin object to enable
them to be inherited. )

[ Hint, Hint - a nice project/experiment here for someone!! ]

Much of this is written up in Doc/ext.ps - but that is the newest and
most incomplete piece of documentation - there is much that won't be
clear to you until you actually TRY adding and modifying xxmodule/
xxobject, or at least read some of the other module/object sources.
[ Read the Reference manual, also, and try pleanty of example IN
Python before you try to do it in C. ]

-- Steve Majewski (804-982-0831) <sdm7g@Virginia.EDU> --
-- UVA Department of Molecular Physiology and Biological Physics --
-- Box 449 Health Science Center Charlottesville,VA 22908 --
[I used to sweep Merce Cunningham's floor - and he had a LOT of floor!]