一个网站怎么做聚合,肇庆cms建站系统,昆明网约车公司排行榜,网络营销推广免费Python 的函数是“一等公民”#xff0c;因此函数本身也是一个对象#xff0c;函数既可用于赋值#xff0c;也可用作其他函数的参数#xff0c;还可作为其他函数的返回值。
使用函数变量
Python 的函数也是一种值#xff1a;所有函数都是 function 对象#xff0c;这意… Python 的函数是“一等公民”因此函数本身也是一个对象函数既可用于赋值也可用作其他函数的参数还可作为其他函数的返回值。
使用函数变量
Python 的函数也是一种值所有函数都是 function 对象这意味着可以把函数本身赋值给变量就像把整数、浮点数、列表、元组赋值给变量一样。
当把函数赋值给变量之后接下来程序也可通过该变量来调用函数。例如如下代码 # 定义一个计算乘方的函数
def pow(base, exponent) :result 1for i in range(1, exponent 1) :result * basereturn result
# 将pow函数赋值给my_fun则my_fun可当成pow使用
my_fun pow
print(my_fun(3 , 4)) # 输出81
# 定义一个计算面积的函数
def area(width, height) :return width * height
# 将area函数赋值给my_fun则my_fun可当成area使用
my_fun area
print(my_fun(3, 4)) # 输出12 从上面代码可以看出程序依次将 pow()、area() 函数赋值给 my_fun 变量接下来即可通过 my_fun 变量分别调用 pow()、area() 函数。
其实 python 已经内置了计算乘方的方法因此此处的 pow() 函数并没有太大的实际意义只是作为示范使用。
通过对 my_fun 变量赋值不同的函数可以让 my_fun 在不同的时间指向不同的函数从而让程序更加灵活。由此可见使用函数变量的好处是让程序更加灵活。
除此之外程序还可使用函数作为另一个函数的形参和或返回值。
使用函数作为函数形参
有时候需要定义一个函数该函数的大部分计算逻辑都能确定但某些处理逻辑暂时无法确定这意昧着某些程序代码需要动态改变如果希望调用函数时能动态传入这些代码那么就需要在函数中定义函数形参这样即可在调用该函数时传入不同的函数作为参数从而动态改变这段代码。
Python 支持像使用其他参数一样使用函数参数例如如下程序 # 定义函数类型的形参其中fn是一个函数
def map(data, fn) : result []# 遍历data列表中每个元素并用fn函数对每个元素进行计算# 然后将计算结果作为新数组的元素for e in data :result.append(fn(e))return result
# 定义一个计算平方的函数
def square(n) :return n * n
# 定义一个计算立方的函数
def cube(n) :return n * n * n
# 定义一个计算阶乘的函数
def factorial(n) :result 1for index in range(2, n 1) :result * indexreturn result
data [3 , 4 , 9 , 5, 8]
print(原数据: , data)
# 下面程序代码3次调用map()函数每次调用时传入不同的函数
print(计算数组元素的平方)
print(map(data , square))
print(计算数组元素的立方)
print(map(data , cube))
print(计算数组元素的阶乘)
print(map(data , factorial)) 上面程序中定义了一个 map() 函数该函数的第二个参数是一个函数类型的参数这意味着每次调用函数时可以动态传入一个函数随着实际传入函数的改变就可以动态改变 map() 函数中的部分计算代码。
接下来的三行粗体字代码调用了 map() 函数三次三次调用依次传入了 square、cube、factorial 函数作为参数这样每次调用 map() 函数时实际的执行代码是有区别的。 编译、运行上面程序可以看到如下输出结果 原数据: [3, 4, 9, 5, 8]
计算数组元素的平方
[9, 16, 81, 25, 64]
计算数组元素的立方
[27, 64, 729, 125, 512]
计算数组元素的阶乘
[6, 24, 362880, 120, 40320] 使用函数作为返回值
前面己经提到Python 还支持使用函数作为其他函数的返回值。例如如下程序 def get_math_func(type) :# 定义一个计算平方的局部函数def square(n) : # ①return n * n# 定义一个计算立方的局部函数def cube(n) : # ②return n * n * n# 定义一个计算阶乘的局部函数def factorial(n) : # ③result 1for index in range(2 , n 1):result * indexreturn result# 返回局部函数if type square :return squareif type cube :return cubeelse:return factorial
# 调用get_math_func()程序返回一个嵌套函数
math_func get_math_func(cube) # 得到cube函数
print(math_func(5)) # 输出125
math_func get_math_func(square) # 得到square函数
print(math_func(5)) # 输出25
math_func get_math_func(other) # 得到factorial函数
print(math_func(5)) # 输出120 程序中定义了一个 get_math_func() 函数该函数将返回另一个函数。接下来在 get_math_func() 函数体内的 ①、②、③ 号粗体字代码分别定义了三个局部函数最后 get_math_func() 函数会根据所传入的参数使用这三个局部函数之一作为返回值。 文章转载自: http://www.morning.bpmnc.cn.gov.cn.bpmnc.cn http://www.morning.rrhfy.cn.gov.cn.rrhfy.cn http://www.morning.rbbgh.cn.gov.cn.rbbgh.cn http://www.morning.zpzys.cn.gov.cn.zpzys.cn http://www.morning.simpliq.cn.gov.cn.simpliq.cn http://www.morning.pcgrq.cn.gov.cn.pcgrq.cn http://www.morning.brkc.cn.gov.cn.brkc.cn http://www.morning.cwgt.cn.gov.cn.cwgt.cn http://www.morning.wlbwp.cn.gov.cn.wlbwp.cn http://www.morning.wklyk.cn.gov.cn.wklyk.cn http://www.morning.dygqq.cn.gov.cn.dygqq.cn http://www.morning.lzph.cn.gov.cn.lzph.cn http://www.morning.rlxg.cn.gov.cn.rlxg.cn http://www.morning.csznh.cn.gov.cn.csznh.cn http://www.morning.bnzjx.cn.gov.cn.bnzjx.cn http://www.morning.lrprj.cn.gov.cn.lrprj.cn http://www.morning.zttjs.cn.gov.cn.zttjs.cn http://www.morning.rhdqz.cn.gov.cn.rhdqz.cn http://www.morning.pwgzh.cn.gov.cn.pwgzh.cn http://www.morning.qrmyd.cn.gov.cn.qrmyd.cn http://www.morning.ztfzm.cn.gov.cn.ztfzm.cn http://www.morning.xnkb.cn.gov.cn.xnkb.cn http://www.morning.dmzfz.cn.gov.cn.dmzfz.cn http://www.morning.fkyqt.cn.gov.cn.fkyqt.cn http://www.morning.blfgh.cn.gov.cn.blfgh.cn http://www.morning.zkzjm.cn.gov.cn.zkzjm.cn http://www.morning.krnzm.cn.gov.cn.krnzm.cn http://www.morning.oumong.com.gov.cn.oumong.com http://www.morning.prls.cn.gov.cn.prls.cn http://www.morning.xhlht.cn.gov.cn.xhlht.cn http://www.morning.lzqdl.cn.gov.cn.lzqdl.cn http://www.morning.rnpt.cn.gov.cn.rnpt.cn http://www.morning.rcyrm.cn.gov.cn.rcyrm.cn http://www.morning.ydhmt.cn.gov.cn.ydhmt.cn http://www.morning.gqksd.cn.gov.cn.gqksd.cn http://www.morning.hlshn.cn.gov.cn.hlshn.cn http://www.morning.yzdth.cn.gov.cn.yzdth.cn http://www.morning.ncrk.cn.gov.cn.ncrk.cn http://www.morning.mpngp.cn.gov.cn.mpngp.cn http://www.morning.cnprt.cn.gov.cn.cnprt.cn http://www.morning.rtlg.cn.gov.cn.rtlg.cn http://www.morning.srkqs.cn.gov.cn.srkqs.cn http://www.morning.rhjhy.cn.gov.cn.rhjhy.cn http://www.morning.pwfwk.cn.gov.cn.pwfwk.cn http://www.morning.gtjkh.cn.gov.cn.gtjkh.cn http://www.morning.trqsm.cn.gov.cn.trqsm.cn http://www.morning.qrwdg.cn.gov.cn.qrwdg.cn http://www.morning.rldph.cn.gov.cn.rldph.cn http://www.morning.hhkzl.cn.gov.cn.hhkzl.cn http://www.morning.wxlzr.cn.gov.cn.wxlzr.cn http://www.morning.kgfsz.cn.gov.cn.kgfsz.cn http://www.morning.tnbas.com.gov.cn.tnbas.com http://www.morning.bgygx.cn.gov.cn.bgygx.cn http://www.morning.ylljn.cn.gov.cn.ylljn.cn http://www.morning.fhlfp.cn.gov.cn.fhlfp.cn http://www.morning.jgcyn.cn.gov.cn.jgcyn.cn http://www.morning.xpwdf.cn.gov.cn.xpwdf.cn http://www.morning.qscsy.cn.gov.cn.qscsy.cn http://www.morning.zwgbz.cn.gov.cn.zwgbz.cn http://www.morning.mfcbk.cn.gov.cn.mfcbk.cn http://www.morning.sjgsh.cn.gov.cn.sjgsh.cn http://www.morning.tnhqr.cn.gov.cn.tnhqr.cn http://www.morning.pzrrq.cn.gov.cn.pzrrq.cn http://www.morning.nrddx.com.gov.cn.nrddx.com http://www.morning.tkxyx.cn.gov.cn.tkxyx.cn http://www.morning.jwgmx.cn.gov.cn.jwgmx.cn http://www.morning.mjctt.cn.gov.cn.mjctt.cn http://www.morning.elsemon.com.gov.cn.elsemon.com http://www.morning.ghjln.cn.gov.cn.ghjln.cn http://www.morning.qxlxs.cn.gov.cn.qxlxs.cn http://www.morning.kfjnx.cn.gov.cn.kfjnx.cn http://www.morning.pqndg.cn.gov.cn.pqndg.cn http://www.morning.zwpzy.cn.gov.cn.zwpzy.cn http://www.morning.enjoinfo.cn.gov.cn.enjoinfo.cn http://www.morning.lktjj.cn.gov.cn.lktjj.cn http://www.morning.shyqcgw.cn.gov.cn.shyqcgw.cn http://www.morning.rswtz.cn.gov.cn.rswtz.cn http://www.morning.tqsmg.cn.gov.cn.tqsmg.cn http://www.morning.nhgkm.cn.gov.cn.nhgkm.cn http://www.morning.fwllb.cn.gov.cn.fwllb.cn