Python老男孩 day15 函数(三) 前向引用之'函数即变量'

时间:2021-04-29 19:16:47

https://www.cnblogs.com/linhaifeng/articles/6113086.html

——————————————————————————————————————

六、前向引用之'函数即变量'

 

def foo():
    print('from foo')
    bar()

foo()

 

运行结果:
NameError: name 'bar' is not defined

def foo():
    print('from foo')
    bar()

def bar():
    print('from bar')
foo()

运行结果:
from foo
from bar

def foo():
    print('from foo')
    bar()

foo()

def bar():
    print('from bar')

运行结果:
NameError: name 'bar' is not defined
from foo