首页 > 其他 > 详细

函数和函数式编程

时间:2020-02-06 01:59:43      阅读:83      评论:0      收藏:0      [点我收藏+]

1、函数的定义

面向过程,面向对象,函数式编程。这是编程的方法论。

面向对象的独门秘技是类,class。面向过程的独门秘技是过程,def。函数式编程的独门秘技是函数,def。

过程就是没有返回值的函数。但是在python中,过程会返回none.

 1 def func1():
 2     #testing1
 3     print("in the func1")
 4     return 0
 5 def func2():
 6     #testing2
 7     print ("in the func2")
 8 
 9 x=func1()
10 y=func2()
11 
12 print ("from func1 return is %s" %x)
13 print ("from func2 return is %s" %y)

2、为什么要用函数?

可扩展,保持一致性,代码重用。

 1 import time
 2 def logger():
 3     time_format="%Y-%m-%d %X"
 4     time_current=time.strftime(time_format)
 5     with open(a.txt,a+) as f:
 6         f.write("%s end action\n" %time_current)
 7 
 8 def test1():
 9     print("in the test1")
10     logger()
11 
12 def test2():
13     print("in the test2")
14     logger()
15 
16 def test3():
17     print("in the test3")
18     logger()
19 
20 test1()
21 test2()
22 test3()

 

函数和函数式编程

原文:https://www.cnblogs.com/lyzfp/p/12267288.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!