[Tutor] defining functions

Remco Gerlich scarblac@pino.selwerd.nl
Mon, 10 Dec 2001 09:39:28 +0100


On  0, Kirk Bailey <highprimate@howlermonkey.net> wrote:
> Gimmee a clue or two on these issues, would you?

A Python 'def' statement is a statement like any other: functions aren't
declared at compile time, but during runtime!


Something like

x = 3
def y(): return 3

Has two different statements that aren't fundamentally different.
First the name 'x' is introduced to refer to 3, then the name 'y' refers to
a new function that returns 3. Both happen at runtime.

So function definitions must be run before they are used, but it doesn't
matter where in the file you place them (as long as they are run at the
right moment).

So

def a():
   return b()
   
def b():
   return 5
   
a()

is ok, since at the moment a() is called, b() was already defined.

However, this is not ok:

def a():
   return b()
   
a()

def b():
   return 5
   
Since now the function a is called before b was defined, so there is no
function b yet.

Hope this clears it up a little. Functions definitions happen at runtime
just like everything else.

-- 
Remco Gerlich