[Tutor] about modules, classes, methods, functions

Alan Gauld alan.gauld at btinternet.com
Mon Sep 3 00:20:52 CEST 2007


"jim stockford" <jim at well.com> wrote

> If I write a little .py file, its name is __main__
> 
> assuming no big nit-picks in the claim above,
> is __main__ a class?

No, its the name of your module when its run as a script 
rather than imported. Its assigned by the interpreter.
(At least i assumev you haven't literally called your 
file __main__.py, that might cause Python some 
confusion!)

> What exactly does it mean "module" and how
> is that different from a class.

A module is a file which contains code.
That code could define functions and/or classes 
It could also define variables as well as contain 
executable code (loops etc)

> Is it sufficient to define a class as some executable
> code that gets run when it's loaded? Someone has
> so defined, but I don't think it's sufficient.

A class definition is executable just like a function definition.
But the execution of the code produces a class object.
By calling that class object you create an instance of 
the class (also, somewhat confusingly called an Object)

> classic definition of a function is some code that
> takes arguments and returns a single value. What's
> the definition of a function in python?

Pretty much the same.
But in the event that the function does not explicitly return 
a value Python provides a default return of None.
Also python return values can include tuples which 
appear like multiple return values:

def maxmin(aSequence):
    mx = max(aSequence)
    mn = min(aSequence)
    return mx,mn

x,n = maxmin([1,2,3,4,5,6,7])

> how is a method different from a function? Is it
> just that a method is a member of a class, i.e. can
> a class have both methods and functions?

It differs in several ways. The primary one is that 
it is defined inside a class. Also the first parameter 
is the instance to which the method applies at run 
time and is not explicitly given at execution time.

class C:
    def meth(self,x):
        print x

c = C()
c.meth(42)

So meth is defined with two parameters but called 
with one.

In pure OOP terms there is another important difference 
between a method and a function. A function is always 
called by name but a method is called by sending a 
"message" to an object. There is nothing in OOP theory 
(and in some languages in practice) that says the 
method must have the same name as the message

Thus:

class C:
    def _aMethod(self): print 'aMethod is running'
    foo = _aMethod
    
c = C()
c._aMethod()
c.foo()

Here we send two messages to c but both execute the same 
method. Other languages (eg. Lisp, Objective C, Eiffel) allow 
us to explicitly name the method invoked by a message and 
have the two take different names. Python doesn't support 
that.

Note:
The concept of sending a message as distinct from 
calling a method is quite fundamental to thinking about 
objects as dynamic emntities within a program. In a theoretically 
pure OOP environment each object would be a separate 
process and the messages would be literal inter-process 
messages. But such a pure approach would be utterly 
impractical on todays technology - far too slow. However the 
concept is extremely important in computer science 
terms since such a machine can be represented 
mathematically and simulations can be created in much 
the same way as electronic engineers can build and 
execute models of circuits. Many software engineering
academics believe that this may be the key to building 
truly reliable and predictable large scale software systems.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld



More information about the Tutor mailing list