zoukankan      html  css  js  c++  java
  • 嵌套函数

    函数在创建之后,没有调用的话函数会一直不动。

    在外部调用func2也不行,因为程序都是从上到下执行的。

    def func1():
        print("alex")
        def func2():
            print("tyu")
        func2() # 若直接在外部调用func1(),只会输出alex,直接跳过func2的函数。
    func1()  
    
    alex
    tyu
    

    子级的函数寻找变量会优先寻找父级的,然后再去找全局变量。

    age = 19
    def func1():
        print(age)
        def func2():
            age = 28
            print(age)
        func2()
    func1()    #输出19 28  

      

    func1() 调用后输出为19 

    age = 19
    def func1():
        print(age)
        def func2():
            age = 28
            print(age)
            func2()
    
    func1()
    # 输出为19

     下面两种均会报错 

    age = 19    
    def func1():
    
        def func2():
            print(age)
        func2()
        age = 28
    func1() 
    age = 19 def func1(): print(age) def func2(): print(age) age = 28 func2() func1()

    中间被修改为age = 28 ,不会输出19.  

    age = 19
    def func1():
        global age
        def func2():
            print(age)
    
        age = 28
        func2()
    
    func1()   #输出为28
    

      

  • 相关阅读:
    Markdown文档示例
    Python网络编程(一)
    JS之客户端检测
    MySQL 多表查询
    MySQL 单表查询
    MySQL 入门
    Python内置方法大全
    010 盒模型
    009 CSS选择器
    008 常用样式
  • 原文地址:https://www.cnblogs.com/Roc-Atlantis/p/8519521.html
Copyright © 2011-2022 走看看