[Tutor] Order of functions

Wesley Chun wesc@deirdre.org
Tue, 10 Jul 2001 15:32:52 -0700 (PDT)


On Tue, 10 Jul 2001, Praveen Pathiyil wrote:

>         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.


yes, the order of function invocations vs. declarations/definitions
is important.  functions which have not been declared/defined cannot
be invoked.

- - - - - - -
foo()

def foo(): pass

FAILS because foo() was called b4 it was def'd.

- - - - - - -
def bar(): foo()

bar()

def foo(): pass

FAILS: reason is the same as above; bar() is okay
        because it was def'd b4 it was called

- - - - - - -
def bar(): foo()

def foo(): pass

bar()

WORKS: because both functions are def'd b4 invocation

- - - - - - -

def foo(): pass

def bar(): foo()

bar()

WORKS: same as above, but look even more convincing because
        foo() is def'd b4 it shows up anywhere else!

- - - - - -

if you have Core Python Programming, this topic can be found
in section 11.3.3 (Forward References) on pp. 340-41.

hope this helps!

-wesley

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Silicon Valley-SF Bay Area Python users group:  http://baypiggies.org

"Core Python Programming", Prentice Hall PTR, December 2000
    http://starship.python.net/crew/wesc/cpp/

wesley.j.chun :: wesc@baypiggies.org
cyberweb.consulting :: silicon.valley, ca
http://www.roadkill.com/~wesc/cyberweb/