0

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?

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by novice20
0

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.

Edited by vegaseat: byte

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.