首页 > 代码库 > 前向引用

前向引用

函数定义 在 函数调用 之前

而函数定义的顺序无关紧要

就如同变量的定义一般

a = 1
b = 2

#两者没什么不同

b = 2
a = 1

  例

def bbb():
    print(‘this is b‘)
    aaa()
def aaa():
    print(‘this is a‘)
bbb()
#--------->
this is b
this is a

  

def aaa():
    print(‘this is a‘)
def bbb():
    print(‘this is b‘)
    aaa()
bbb()
#----------->
this is b
this is a

  不可以这样

def bbb():
    print(‘this is b‘)
    aaa()
bbb()
def aaa():
    print(‘this is a‘)
#--------->
Traceback (most recent call last):
this is b
  File "E:/pycharm/TEST.py", line 600, in <module>
    bbb()
  File "E:/pycharm/TEST.py", line 599, in bbb
    aaa()
NameError: name ‘aaa‘ is not defined

  

前向引用