zoukankan      html  css  js  c++  java
  • python基础--函数作用域

    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())
    如果我失败了,至少我尝试过,不会因为痛失机会而后悔
  • 相关阅读:
    Cocos2d-x游戏移植到Android平台
    Alice and Bob
    Hamming Codes
    Fire逃生
    Java中面向对象的理解
    常见的几个算法
    数组的介绍
    Java 中的数据类型
    Java 初相识
    JavaScript 数据类型 (续)
  • 原文地址:https://www.cnblogs.com/tangcode/p/10984094.html
Copyright © 2011-2022 走看看