Hi all,
Is there any way to do forward referencing or function prototyping in python..?
Being an interpreted language, is lack of forward referencing a drawback of python?

Recommended Answers

To avoid problems define all your functions in the beginning of a program (more readable anyway!) ...

def f1():
    print('f1')
    # call function f2
    f2()

def f2():
    print('f2')
    f3()

def f3():
    print('f3')

# all function have been defined
# now this will work
f1()

This will give …

Jump to Post

All 2 Replies

To avoid problems define all your functions in the beginning of a program (more readable anyway!) ...

def f1():
    print('f1')
    # call function f2
    f2()

def f2():
    print('f2')
    f3()

def f3():
    print('f3')

# all function have been defined
# now this will work
f1()

This will give problems ...

def f1():
    print('f1')
    # call function f2
    f2()

# this will give
# NameError: global name 'f2' is not defined
f1()

def f2():
    print('f2')
    f3()

def f3():
    print('f3')

In the Python interpreter functions are compiled (byte code), but not used until called.

Following these simple rules, I have never encountered a drawback.

Thank you vegaseat :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.