第二天 Python3.4.2 函数
1.什么是函数
1.1 函数是组织好的,可以重复使用的,实现单一功能的代码段
1.2 函数有输入(参数)和输出(返回值)
1.3 python中,函数是一等对象(first clas)
函数的定义与调用:
In [33]: def fn(): ....: pass ....: In [34]: fn() In [35]: def fn(): ....: print ('test fn') ....: In [36]: fn() test fn
如果里面有赋值,创建新的变量,没有赋值,就用原来的变量的值。
In [43]: lst=[1,2,3] In [44]: def append(item): ....: lst.append(item) ....: In [45]: append(4) In [46]: lst Out[46]: [1, 2, 3, 4]
1.1 位置参数和关键字参数
In [1]: def add(x,y): ...: print("x={0}".format(x)) ...: print("y={0}".format(y)) ...: return x+y ...: In [2]: add(1,2) #位置参数 通过参数传递的位置决定 x=1 y=2 Out[2]: 3 In [3]: add(x=3,y=4) #关键字参数 通过参数名称来决定 x=3 y=4 Out[3]: 7 In [4]: add(y=2,x=3) #关键字参数,关键字参数,是通过参数名称来决定的 x=3 y=2 Out[4]: 5
1.2 关键字参数和位置参数的的混合使用:
注意:混合使用的时候,关键字参数必须在位置参数之后。
In [5]: add(1,y=2) x=1 y=2 Out[5]: 3 In [6]: add(x=1,2) File "<ipython-input-6-f55da436a67c>", line 1 add(x=1,2) ^ SyntaxError: non-keyword arg after keyword arg
位置参数和关键字参数是有其所在的位置决定。
1.3 默认参数:
In [1]: def inc(x,i=1): ...: return x + i ...: In [2]: inc(5) Out[2]: 6 In [3]: inc(5, 3) Out[3]: 8
当默认参数和位置参数在一块使用的时候,世界是美好的。
使用mysql连接的时候,这个功能用法就很好,root 和 密码一样,只需要修改host
1.4 可变参数:
In [9]: def sum(lst): #参数是列表 ...: ret = 0 ...: for x in lst: ...: ret += x ...: return ret In [11]: sum([1,2,3,4,5]) Out[11]: 15
1.5 可变位置参数
In [16]: def sum(*args): ....: ret = 0 ....: print (args) ....: for x in args: ....: ret +=x ....: return ret In [18]: sum(1,2,3,4,5) #元组 (1, 2, 3, 4, 5) Out[18]: 15
In [21]: sum(5,4,3,2,1)
(5, 4, 3, 2, 1)
Out[21]: 15
1.6 可变关键字参数:
*kwargs 表示
In [24]: def print_info(**kwargs): ....: for k,v in kwargs.items(): ....: print ('{0}==>{1}'.format(k,v)) ....: In [25]: print_info(a=1,b=2) a==>1 b==>2 In [26]: print_info(a=1,b=2,c=3) c==>3 a==>1 b==>
1.7 可变参数函数在定义的时候,就决定了参数是位置参数还是关键字参数
可变参数和可变位置参数的混合使用:
In [31]: def print_info(*args,**kwargs): ....: for x in args: ....: print (x) ....: for k,v in kwargs.items(): ....: print ('{0}====>{1}'.format(k,v)) ....: In [32]: print_info(1,2,3,4,a=5,b=6) 1 2 3 4 a====>5
位置参数,可变参数,可变关键字参数结合:
In [38]: def print_info(x,y,*args,**kwargs): print ('x = {0}'.format(x)) print ('y = {0}'.format(y)) for x in args: print (x) for k,v in kwargs.items(): print ('{0} ====>{1}'.format(k,v)) ....: In [39]: print_info(1,2,3,4,5,6,a=7,b=8) x = 1 y = 2 3 4 5 6 a ====>7 b ====>8
不允许关键字参数在最后面:
In [40]: def print_info(*args,**kwargs,x,y): print ('x = {0}'.format(x)) print ('y = {0}'.format(y)) for x in args: print (x) for k,v in kwargs.items(): print ('{0} ====>{1}'.format(k,v)) ....: File "<ipython-input-40-77353f341e5d>", line 1 def print_info(*args,**kwargs,x,y): ^ SyntaxError: invalid syntax
符合关键字参数的赋值:
1 In [41]: def print_info(x,y=0,*args,**kwargs): 2 print ('x = {0}'.format(x)) 3 print ('y = {0}'.format(y)) 4 for x in args: 5 print (x) 6 for k,v in kwargs.items(): 7 print ('{0} ====>{1}'.format(k,v)) 8 ....: 9 10 In [42]: print_info(1, 2, 4, 5, 6, a=7, b=8) 11 x = 1 12 y = 2 13 4 14 5 15 6 16 a ====>7 17 b ====>8
In [43]: print_info(1, a=7, b=8) x = 1 y = 0 a ====>7 b ====>8
总结:
规则
1:可变参数在位置参数之后
2.默认参数在其他参数之后
3.可变位置参数在非可变参数之前,非可变参数必须以关键字参数传参
4可变关键字参数必须在非可变参数之后
老光给的:
1.非默认非可变参数,可变位置参数,可变关键字参数
2.默认参数不要和可变参数放在一起使用
2.参数解包
In [49]: def add(x,y): print('x is {0}'.format(x)) print('y is {0}'.format(y)) return x+y ....: In [50]: lst=[1,2] In [51]: add(lst[0],lst[1]) x is 1 y is 2 Out[51]: 3 In [52]: add(*lst) x is 1 y is 2 Out[52]: 3
In [57]: d = {'x': 1, 'y': 2} In [58]: add(**d) x is 1 y is 2 Out[58]: 3
In [59]: add(*[1,2,3,4,5]) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-59-89024c0349d7> in <module>() ----> 1 add(*[1,2,3,4,5]) TypeError: add() takes 2 positional arguments but 5 were given #2个参数给了5个 In [60]: add(**{'x':1,'y':2,'z':3}) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-60-61d57683cd86> in <module>() ----> 1 add(**{'x':1,'y':2,'z':3}) TypeError: add() got an unexpected keyword argument 'z' #多个z In [62]: add(**{'x':1}) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-62-9feb3d25ee96> in <module>() ----> 1 add(**{'x':1}) TypeError: add() missing 1 required positional argument: 'y' #少了一个参数
3:默认参数的坑:
In [64]: def fn(lst=[]): ....: lst.append(1) ....: print(lst) ....: In [65]: fn() [1] In [66]: fn() [1, 1] In [67]: fn() [1, 1, 1] In [68]: fn() [1, 1, 1, 1]
如果我只是输出一个1
def fn(lst = None): #在jypyter 测试通过 if lst is None: lst = [] lst.append(1) print(lst) fn() [1] In [9]: fn() [1] In [10]: fn() [1]
4 函数的返回值