函数与函数式编程1.介绍: 在过去的十年间,你们广为熟知的编程方法无非两种:面向对象和面向过程,其实,不管哪一种,都是一种编程的规范或者是如何编程的方法论。 而现在,一种更为古老的编程方式:函数式编程,以其不保存状态,不修改变量等特性从新进入人们的视野。 下面咱们就来依次了解这一传统的编程理念,让咱们从基本的函数概念开始。2.函数定义: 初中数学函数定义: 通常的,在一个变化过程当中,若是有两个变量x和y,而且对于 x 的每个肯定的值,y 都有惟一肯定的值与其对应, 那么咱们就把 x 称为自变量,把 y 称为因变量,y 是 x 的函数。自变量 x 的取值范围叫作这个函数的定义域 编程语言中函数定义: 函数是逻辑结构化和过程化的一种编程方法。 python 中函数定义方法:def test(x):"The function definitions"x+=1return xdef:定义函数的关键字 test:函数名 ():内可定义形参"":文档描述(非必要,可是强烈建议为你的函数添加描述信息) x+=1: 泛指代码块或程序处理逻辑return:定义返回值 补充: 函数式编程就是:先定义一个数字函数,而后按照这个数学模型用编程语言去实现它。至于具体如何实现和这么作的好处,后续会详细介绍
1.面向对象:类 -----》class2.面向过程:过程 ---》def3.函数式编程:函数 ---》defy = 2x 函数把逻辑结构化过程化的一种方法# 定义函数def func1(): """testing1""" print('in the func1') return 0# 定义过程def func2(): '''testing2''' print('in the func2')# 这是定义了一个过程 # 调用函数,x接收返回值0x=func1()# 调用过程,没有接收返回值,按理说,应该返回什么?y=func2()# 过程就是没有返回值的函数print('from func1 return is %s' %x)print('from func2 return is %s' %y)---> in the func1---> in the func2---> from func1 return is 0---> from func2 return is None# 在没有返回值的状况下,Python的解释器隐式的返回了 None,在Python当中,过程也没当成函数了# 不是说有 return 就叫函数式编程了
3. 为什么使用函数 没有函数的编程只是在写逻辑(功能),想脱离函数,重用你的逻辑,惟一的办法就是拷贝 例一: 假设咱们编写好了一个逻辑(功能),用来以追加的方式写日志: with open('a.txt','ab') as f: f.write('end action') 如今有三个函数,每一个函数在处理完本身的逻辑后,都须要使用上面这个逻辑,那么惟一的方法就是,拷贝三次这段逻辑def test1():print 'test1 starting action...'with open('a.txt','ab') as f: f.write('end action')def test2():print 'test2 starting action...'with open('a.txt','ab') as f: f.write('end action')def test3():print 'test3 starting action...'with open('a.txt','ab') as f: f.write('end action') 那么假设有 > N 个函数都须要使用这段逻辑,你能够拷贝 N 次吗? 例二: 优化后的代码def logger_test(): with open('a.txt','ab') as f: f.write('end action') 复制代码
1 >>with open('a.txt','ab') as f: f.write('end action')def test1(): print('test1 starting action...') with open('a.txt','ab') as f: f.write('end action')def test2(): print('test2 starting action...') with open('a.txt','ab') as f: f.write('end action')def test3(): print('test3 starting action...') with open('a.txt','ab') as f: f.write('end action')
2 >>def logger(): with open('a.txt','a+') as f: f.write('end action\n')def test1(): print('test1 starting action...') logger()def test2(): print('test2 starting action...') logger()def test3(): print('test3 starting action...') logger() test1() test2() test3()
3 >>import timedef logger(): time_format = '%Y-%m-%d %X' # X 表明小时 分钟 秒 time_current = time.strftime(time_format) with open('a.txt','a+') as f: f.write('%s end action\n' %time_current)def test1(): print('test1 starting action...') logger()def test2(): print('test2 starting action...') logger()def test3(): print('test3 starting action...') logger() test1() test2() test3()# 函数的第二个好处就是,可扩展,保持一致性例三: 需求变了(让咱们来为日志加上时间吧)import timedef logger_test(): timeformat='%-%m-%d %x'time_current = time.strftime(time _format) with open('a.txt", 'ab') as f:f.write('time %s end action' %time_current)def test1():print('test1 starting action....') logger_test()def test2():print('test2 starting action....') logger_test()def test3():print('test3 starting action...') logger_test() 总结例二和例三可归纳使用函数的三大优势1.代码重用2.保持一致性3.可扩展性