首页 > 其他 > 详细

函数式编程

时间:2018-09-05 19:17:42      阅读:183      评论:0      收藏:0      [点我收藏+]

Python中函数定义方法:

def test(x):
"the function definitions"
x+=1
return x

def:定义函数的关键字
test:函数名
"":文档描述
x+=1: 泛指代码块或程序处理逻辑
return : 定义返回值

 

函数与过程的区别
函数有返回值,过程没有返回值。过程就是没有返回值的函数
#函数
def func1():
"""testing1"""
print(‘in the func1‘)
return 0
#过程
def func2():
‘‘‘testing2‘‘‘
print(‘in the func2‘)

x=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

 


函数返回值:

def test1():
print(‘in the test1‘)

def test2():
print(‘in the test2‘)
return 0
def test3():
print(‘in the test3‘)
#return 1,‘hello‘,[‘alex‘,‘wupeiqi‘],{‘name‘:‘alex‘}
return test2

x=test1()
y=test2()
z=test3()
print(x)
print(y)
print(z)

结果:
in the test1
in the test2
in the test3
None
0
<function test2 at 0x0048B738>

总结:
返回值数=0:返回None
返回值数=1:返回object
返回值数>1:返回tuple

 


参数详解
def test(x,y,z):
print(x)
print(y)
print(z)

# test(y=2,x=1) #与形参顺序无关
# test(1,2) #与形参一一对应
#test(x=2,3)
test(3,z=2,y=6) #关键参数一定在位置参数之后

结果
3
6
2

 

函数式编程

原文:https://www.cnblogs.com/tengtianshan/p/9593753.html

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