码迷,mamicode.com
首页 > 编程语言 > 详细

python基础--函数作用域

时间:2019-06-06 11:59:24      阅读:92      评论:0      收藏:0      [点我收藏+]

标签:函数作用域   bar   col   nbsp   内存地址   使用   The   直接   基础   

name="alex"
def foo():
    name="tang"
    #print(name)
    def bar():

        print(name)
    return bar#函数名代表得就是一个函数的内存地址
# a=foo()
# print(a)
# print(a())
foo()()#由于函数bar包含在函数foo()中,所以bar会在自身查找打印的变量,如果没找到去往上一级查找,最后查找全局
#python编译器是按照顺序加载的,在调用foo()时函数bar被加载,所以必须要按照加载顺序调用函数,return返回的是函数名及函数在内存中的地址,所以可以直接使用foo()()调用
# def test1(): # print(‘in the test1‘) # return 1 # # def test(): # print("in the test") # return test1() # # #print(test) # a=test() # print(a) # def test1(): # print(‘in the test1‘) # #return 1 # # def test(): # print("in the test") # return test1 # # #print(test) # res=test() # print(res())

 

python基础--函数作用域

标签:函数作用域   bar   col   nbsp   内存地址   使用   The   直接   基础   

原文地址:https://www.cnblogs.com/tangcode/p/10984094.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!