[Tutor] Order of functions

D-Man dsh8290@rit.edu
Tue, 10 Jul 2001 11:54:27 -0400


On Tue, Jul 10, 2001 at 07:00:39PM +0530, Praveen Pathiyil wrote:
| Hi,
| 
|         Is the order of functions important in python ?
|         Suppose that i have a class X. If i have functions a, b,c
|         and if 'a' calls 'b', is it necessaruy to define 'b' before
|         'a' or before 'b' is being called.

Regardless of your programming environment, any
function/class/module/whatever must be defined before it can be used.
In some environments, such as C C++ and Java, the compiler looks at
all the source and generates all the machine code so the order of the
text in a source file is irrelevant -- the compiler defines all
functions before the program is run.  In Python, because it executes
the code while it is "compiling" it (creating the function objects --
I'm trying to use the same terminology as with the other environments)
so it must have the definition of some function before it is called.

The result :  if you have a class that defines several methods, and
those methods call each other, it doesn't matter what order the
methods are in the source code because they won't be executed until
after an instance had been created, which can't happen until the class
object is created which includes creating all the method objects.

I know this sounds a bit confusing, but if you trace through what the
interpreter does as it executes your code you will see what needs to
be done first and what needs to be done later.

HTH,
-D