编程语言
首页 > 编程语言> > Python学海无涯路【第10回】:前向引用

Python学海无涯路【第10回】:前向引用

作者:互联网

文章目录


1、函数即变量

把函数声明当成C++中的变量声明,使用前要先定义


例一:

#funcA当作是变量,函数体当作其初始值
def funcA():
    print ("in the funcA")
    funcB()			#funcB未定义不报错
    
funcA()     #读这行,funcA定义了不报错。

报错:NameError: name ‘funcB’ is not defined


例二:

def funcA():
    print ("in the funcA")
    funcB()
def funcB():
    print("in the funcB")

funcA()

输出:
in the funcA
in the funcB


例三:

def funcA():
    print ("in the funcA")
    funcB()

funcA()
def funcB():
    print("in the funcB")

报错:NameError: name ‘funcB’ is not defined

标签:10,变量,学海无涯,Python,funcB,funcA,报错,print,def
来源: https://blog.csdn.net/chuhe163/article/details/90048926