Python--12 内嵌函数和闭包

内嵌函数/内部函数

  >>> def fun1():
  ... print(‘fun1()正在调用‘)
  ... def fun2():
  ... print(‘fun2()正在被调用‘)
  ... fun2()
  ...
  >>> fun1()
  fun1()正在调用
  fun2()正在被调用

内部函数作用域在外部函数之内

  >>> fun2()
  Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  NameError: name ‘fun2‘ is not defined

闭包 closure

 闭包函数式编程的一种重要语法结构

 函数式编程是一种编程范式,对代码进行抽象提炼和概括,使代码重用性变高

 python闭包在表现形式表现为 如果在一个内部函数里对外部作用域(但不是全局作用域)的变量进行引用,内部函数就被认为是闭包

  >>> def FunX(x):
  ... def FunY(y):
  ... return x * y
  ... return FunY
  ...
  >>> i = FunX(8)
  >>> i
  <function FunX.<locals>.FunY at 0x7f5f41b05b70>
  >>> type(i)
  <class ‘function‘>
  >>> i(5)
  40
  >>> FunX(8)(5)
  40
  >>> FunY(5)
  Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  NameError: name ‘FunY‘ is not defined

  >>> def Fun1():
  ... x=5
  ... def Fun2():
  ... x *= x
  ... return x
  ... return Fun2()
  ...
  >>> Fun1()
  Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 6, in Fun1
  File "<stdin>", line 4, in Fun2
  UnboundLocalError: local variable ‘x‘ referenced before assignment

 列表没有存放在栈里

  >>> def Fun1():
  ... x = [5]
  ... def Fun2():
  ... x[0] *= x[0]
  ... return x[0]
  ... return Fun2()
  ...
  >>> Fun1()
  25

nonlocal 关键字

  >>> def Fun1():
  ... x=5
  ... def Fun2():
  ... nonlocal x
  ... x *= x
  ... return x
  ... return Fun2()
  ...
  >>> Fun1()
  25

时间: 2024-08-08 13:54:02

Python--12 内嵌函数和闭包的相关文章

Python 学习笔记 -- 内嵌函数、闭包、匿名函数、高阶函数map、高阶函数filter、高阶函数reduce

1 #------------------------------内嵌函数------------------------------ 2 #内嵌函数就是在函数内部定义函数 3 #实例一 4 print("#------------------------------内嵌函数------------------------------") 5 def funOutOne(): 6 x = 5 7 def funIn(): 8 x = 3 9 print("My funOutO

内嵌函数和闭包

函数的嵌套 python的函数支持内嵌,即在函数中定义函数 >>> def fun1(): print('fun1()正在被调用') def fun2(): print('fun2()正在被调用') fun2() >>> fun1() fun1()正在被调用 fun2()正在被调用 内嵌函数的作用域在外部函数之内,即fun2只能在fun1之内调用. >>> fun2() Traceback (most recent call last): File &

Python内嵌函数

局部变量 def discount(price, rate): final_price = price * rate return final_price old_price = float(input('请输入原价:'))    全局变量 rate = float(input('请输入折扣率:')) new_price = discount(old_price, rate) print('打折后的价格是:',new_price) print('打印局部变量final_price的值:',fin

Python 常用内置函数

abs 取绝对值 print(abs(-1)) #结果1 all(...) all(iterable) -> bool Return True if bool(x) is True for all values x in the iterable. If the iterable is empty, return True. 如果iterable的所有元素不为0.''.False或者iterable为空,all(iterable)返回True,否则返回False:函数等价于: 1 def all

Python3基础 内嵌函数 简单示例

? ???????Python : 3.7.0 ?????????OS : Ubuntu 18.04.1 LTS ????????IDE : PyCharm 2018.2.4 ??????Conda : 4.5.11 ???typesetting : Markdown ? code """ @Author : 行初心 @Date : 18-9-24 @Blog : www.cnblogs.com/xingchuxin @Gitee : gitee.com/zhichengji

Makefile---make内嵌函数及make命令显示 (九)

原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuewei/ 这一节我们讲一下make的函数,在之前的章节已经讲到了几个函数:wildcard.patsubst.notdir.shell等.一般函数的调用格式如下: $(funcname arguments) 或 $(funcname arguments) 其中funcname是需要调用函数的函数名称,应该是make内嵌函数:arguments是函数参数,参数和函数名之间使用空格分割,如果存在多个参 数时

3.MVC框架开发(Razor内嵌函数)

1.建立没有返回值的Razor内嵌函数(但是能直接输出内容) 必须以@符号开头,没有返回值但能直接输出内容,比如: @helper showTitle(string strTitle){ if(strTitle.Length > 8){ @(strTitle.Substring(0,8)+"...") //输出内容 }else{ @strTitle //输出内容 } } 2.建立有返回值的Razor内嵌函数 必须以@functions开头且里面是一个静态方法,比如通过图书ISBN

Makefile所有内嵌函数

一.文本处理函数以下是GNU make内嵌的文本(字符串)处理函数.1       $(subst FROM,TO,TEXT) 函数名称:字符串替换函数—subst. 函数功能:把字串“TEXT”中的“FROM”字符替换为“TO”. 返回值:替换后的新字符串. 示例: $(subst ee,EE,feet on the street) 替换“feet on the street”中的“ee”为“EE”,结果得到字符串“fEEt on the strEEt”.2       $(patsubst 

从头开始写项目Makefile(十):make内嵌函数及make命令显示

[版权声明:转载请保留出处:blog.csdn.net/gentleliu.Mail:shallnew at 163 dot com] 这一节我们讲一下make的函数,在之前的章节已经讲到了几个函数:wildcard.patsubst.notdir.shell等.一般函数的调用格式如下: $(funcname arguments) 或 $(funcname arguments) 其中funcname是需要调用函数的函数名称,应该是make内嵌函数:arguments是函数参数,参数和函数名之间使