函数及函数式编程:
面向对象: 特征 :类 定义方式 class
面向过程: 特征: 过程 定义方式 def
函数式编程:特征 函数 定义方式 def
1. 函数结构:
1 def test1(x): 2 "this is a test" 3 x += 1 4 return x 5 6 print(test1(5)) 7 8 ''' 9 def 定义函数的关键字 10 test1 函数名 11 (x) 定义形参 12 " " 函数描述,结构规范 通过help()查看 13 x+=1 函数体 14 return x 返回一个x 15 '''
使用help查看 fun 函数 ,help(fun)
2.函数和过程的区别:
1 # 函数 2 def fun1(): 3 """noknow""" 4 print("nobody") 5 return 0 6 7 fun1() 8 9 # 过程 10 11 def fun2(): 12 "nobody1" 13 print("nobody2") 14 15 fun2() 16 17 ''' 18 过程是没有返回值的函数 19 但是在python 中没有返回值会隐式的返回 None 20 '''
3.使用函数的好处:可扩展,代码复用,一致性
1 import time 2 3 4 def fun1(): 5 time_format = "%d - %m - %Y %X" # 时间格式 6 time_current = time.strftime(time_format) 7 with open("a.txt","a+") as f: 8 f.write("%s this is a pig " % time_current) 9 10 11 12 def test1(): 13 "test1" 14 print("in the test1") 15 fun1() 16 17 def test2(): 18 "test2" 19 print("in the test2") 20 fun1() 21 22 def test3(): 23 "test3" 24 print("in the test3") 25 fun1() 26 27 test1() 28 test2() 29 test3() 30 ''' 31 27 - 09 - 2018 22:40:32 this is a pig 32 27 - 09 - 2018 22:40:32 this is a pig 33 27 - 09 - 2018 22:40:32 this is a pig 34 '''
4.函数返回值,函数的返回值是为了得到函数的执行结果,比如返回True False 之类的
1 def test1(): 2 print("in the test1") 3 return 0 # 程序碰见return 就会终止运行 4 5 def test2(): 6 "test2" 7 print("in the teas2") 8 return test1() # 函数的返回值也可以是一个函数。这个值可以理解为一个对象 9 10 def test3(): 11 "test3" 12 print("in the test3") 13 return 1,'hello',['ad','ca'],{'name':'alex'} 14 15 16 x = test1() 17 print(x) 18 y = test2() 19 z = test3() 20 21 print(y) # None 没有返回值就返回None 22 print(z) # (1, 'hello', ['ad', 'ca'], {'name': 'alex'}) python 返回一个值,如果要返回多个内容,以元组的形式返回 23 24 ''' 25 in the test1 26 0 27 in the teas2 28 in the test1 29 in the test3 30 0 31 (1, 'hello', ['ad', 'ca'], {'name': 'alex'}) 32 '''
5.位置参数的函数
1 def test1(x,y): 2 "add" 3 print(x+y) 4 5 test1(3,4) # 7
x,y 为形参,不调用时不占空间
6.关键字参数
1 def test1(x,y): 2 "nothing" 3 print(x) 4 print(y) 5 print(x) 6 7 x = 1 8 y = 2 9 10 test1(y = y,x = x) # 关键字调用,与形参一一对应 11 12 ''' 13 1 14 2 15 1 16 '''
8. 位置参数和关键字参数一起调用时应该注意位置参数在关键字参数之前,同时注意已经赋值过的形参不可以在用关键字赋值
1 def test1(x,y): 2 "nothing" 3 print(x) 4 print(y) 5 print(x) 6 7 8 test1(2,y = 5) 9 10 ''' 11 2 12 5 13 2 14 ''' 15 16 # test1(x=2,3) # 报错 SyntaxError: positional argument follows keyword argument 17 # test1(3,x=1) # 报错 TypeError: test1() got multiple values for argument 'x'
(关键参数必须写在位置参数之后)
9.默认参数
1 def test1(x,y=2): 2 "test1" 3 print(x) 4 print(y) 5 6 test1(3) 7 ''' 8 3 9 2 10 '''
默认参数特点,调用函数的时候,默认参数非必须传递
用途:设置默认安装值
连接数据库的端口号
等等。。。。。
10.参数组
*args 把n个参数转换成元组的形式
def test10(*args): "test10" print(args) test10(1,2,3,4,5,6) test10([1,2,3,4,5],[1,2]) test10([1,2,3,4]) test10(*[1,2,3,4]) #(1, 2, 3, 4, 5, 6) #([1, 2, 3, 4, 5], [1, 2]) #([1, 2, 3, 4],) #(1, 2, 3, 4) # 返回的值是一个元组,如果元组只有一个元素,比如tuple('1') 会输出 ('1',) 这里的列表也可以看作一个元素 # 同时注意 int 类型是不可以迭代的 所以tuple(1)会报错
**kwargs 把n个关键字参数转换成字典的形式
def test10(**kwargs): "test10" print(kwargs) print(kwargs['name']) test10(name='Ri',age = 88) test10(**{'name':'Ri','age':88})
11.操作
1 def test(name,age=18,*args,**kwargs): 2 print('结果') 3 print(name) 4 print(age) 5 print(args) 6 print(kwargs) 7 8 test('ri') 9 ''' 10 结果 11 ri 12 18 13 () 14 {} 15 ''' 16 17 test('ri',88,'qq','weix',A = 'mayuns') 18 ''' 19 ri 20 88 21 ('qq', 'weix') 22 {'A': 'mayuns'} 23 ''' 24 # 注意函数赋值顺序是位置参数,默认参数,位置参数组(转换成元组),关键字参数组(转换成字典)