2021
02-01
02-01
Python 中Operator模块的使用
Python中的Operator模块可以让它支持函数式编程。1计算函数假设我们需要一个计算阶乘的函数,一般做法是使用递归。如果使用函数式编程,可以有两种方式,一种lambda,另一种使用Operator模块中的算术函数。我们做个比较。首先使用lambda方式来实现:fromfunctoolsimportreducedeffact(n):returnreduce(lambdaa,b:a*b,range(1,n+1))这里用到了reduce方法,reduce()函数语法:reduce(function,iterable...
继续阅读 >