上一页 1 ··· 311 312 313 314 315 316 317 318 319 ··· 367 下一页
摘要: 1、 >>> def a(): print("begin eating!") >>> print("starting!") starting! >>> a() begin eating! >>> print("ending") ending ↓ >>> def a(): print("startin 阅读全文
posted @ 2021-03-07 10:59 小鲨鱼2018 阅读(51) 评论(0) 推荐(0) 编辑
摘要: 1、 >>> def a(): x = 200 def b(): print("inner:",x) return b() >>> a() inner: 200 2、 >>> def a(): x = 100 def b(): x = 300 print("inner:",x) return b() 阅读全文
posted @ 2021-03-06 16:55 小鲨鱼2018 阅读(129) 评论(0) 推荐(0) 编辑
摘要: 1、 内嵌函数中,内层函数可以调用外层函数的局部变量 >>> def a(): x = 8 def b(): print(x + 3) return b() >>> a() 11 >>> def a(): x = 10 def b(y): print(x + y) return b >>> a()( 阅读全文
posted @ 2021-03-06 16:37 小鲨鱼2018 阅读(1115) 评论(0) 推荐(0) 编辑
摘要: 1、 内嵌函数的内层函数的作用域在外层函数之内,不能在外层函数外调用内层函数。 >>> def a(): ## 外层函数a(),内层函数b(). print("hello world!") def b(): print("good morning!") return b() >>> a() hell 阅读全文
posted @ 2021-03-06 16:25 小鲨鱼2018 阅读(605) 评论(0) 推荐(0) 编辑
摘要: 1、 >>> def a(): print("hello world!") >>> a <function a at 0x000002CE49DCB550> >>> a() hello world! 2、 >>> def a(): print("hello world!") def b(): pri 阅读全文
posted @ 2021-03-06 15:57 小鲨鱼2018 阅读(741) 评论(0) 推荐(0) 编辑
摘要: 1、 >>> def a(): x = 10 def b(): x = x + 8 print(x) return b >>> a() <function a.<locals>.b at 0x000001B47F4D8040> >>> a()() Traceback (most recent cal 阅读全文
posted @ 2021-03-05 22:27 小鲨鱼2018 阅读(475) 评论(0) 推荐(0) 编辑
摘要: 1、定义 如果在一个内部函数里,对在外部作用域但不是全局作用域的变量进行了引用,那么内部函数就被认为是闭包。简言之,就是嵌套函数的环境下,内部函数引用了外部函数的局部变量,这个内层函数就被认为是闭包。 or 在一个外函数中定义了一个内函数,内函数中运用了外函数的局部变量,并且外函数的返回值是内函数的 阅读全文
posted @ 2021-03-05 22:25 小鲨鱼2018 阅读(54) 评论(0) 推荐(0) 编辑
摘要: 1、一般情况下,无法利用局部变量修改全局变量的值 demo: >>> x = 10 ## 首先定义全局变量 >>> def a(): x = 10000 ## 尝试利用局部变量修改全局变量 print(x) >>> a() ## 局部变量 10000 >>> x ## 全局变量仍然为10 10 2、 阅读全文
posted @ 2021-03-05 22:05 小鲨鱼2018 阅读(6025) 评论(0) 推荐(0) 编辑
摘要: python中允许在函数内定义另一个函数,这种函数称为内嵌函数或者内部函数。 1、 >>> def a(): ## 外层函数 print("hello world!") def b(): ## 内层函数 print("xxxxxxx!") return b() >>> a() hello world 阅读全文
posted @ 2021-03-05 17:04 小鲨鱼2018 阅读(179) 评论(0) 推荐(0) 编辑
摘要: 1、一般情况下,无法在函数内对全局变量进行修改 >>> x = 10 >>> def a(): x = 1000 print(x) >>> a() 1000 >>> x 10 2、利用global关键字,在函数内对全局变量进行修改 >>> x = 10 >>> def a(): global x x 阅读全文
posted @ 2021-03-05 16:36 小鲨鱼2018 阅读(4096) 评论(0) 推荐(0) 编辑
上一页 1 ··· 311 312 313 314 315 316 317 318 319 ··· 367 下一页