zoukankan      html  css  js  c++  java
  • Python老男孩 day16 函数(五) 函数的作用域

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

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

    八、函数的作用域

    def test1():
        print('in the test1')
    def test():
        print('in the test')
        return test1
    
    res=test()   #res接收到test1
    print(res)   #相当于print(test1)
    print(res()) #res()相当于运行test1()

    运行结果:
    in the test
    <function test1 at 0x0000021BA1C01EA0> #输出test1的内存地址
    in the test1
    None #test1没有返回值,所以print输出None

    name = 'alex'
    def foo():
        name='linhaifeng'
        def bar():
            name='wupeiqi'
            print(name)
        return bar
    a=foo()
    print(a)
    a()

    运行结果:
    <function foo.<locals>.bar at 0x0000020F90680C80> #bar的内存地址
    wupeiqi

    name = 'alex'
    def foo():
        name='linhaifeng'
        def bar():
            print(name)
        return bar
    
    a=foo()
    print(a)
    a()

    运行结果:
    <function foo.<locals>.bar at 0x0000012D3D170C80>
    linhaifeng

    name='alex'
    
    def foo():
        name='lhf'
        def bar():
            name='wupeiqi'
            def tt():
                print(name)
            return tt
        return bar
    
    func=foo()   #func=bar
    func()()     #func()=bar()=tt      func()()=tt()

    运行结果:
    wupeiqi

  • 相关阅读:
    Sqli-labs less 25a
    Sqli-labs less 26
    Sqli-labs less 26a
    Sqli-labs less 27
    Sqli-labs less 27a
    Sqli-labs less 28
    Sqli-labs less 28a
    Python3之sys模块
    Python3之os模块
    Python3之XML模块
  • 原文地址:https://www.cnblogs.com/zhuhemin/p/9108406.html
Copyright © 2011-2022 走看看