Python 學習筆記(十三)Python函數(二),學習筆記python函數
參數和變數
1 >>> def foo(a,b): #函數是一個對象2 return a+b3 4 >>> p =foo #對象指派陳述式。將foo函數賦值給p這個變數5 >>> foo(4,5)6 97 >>> p(4,5) 變數p就指向了foo這個函數8 99 >>>
按引用傳遞參數
按照順序進行賦值,函數中的變數a就指向了x,x是第一個實參,a這個參數指向了x所引用的對象,並不是把3這個數複製一個放到函數中,這種調用對象的方式,稱之為按引用傳遞。
1 >>> x,y=3,4 #x,y兩個變數分別指向了3和4 2 >>> foo(x,y) #函數中有兩個參數a和b 3 7 4 >>> def bar(x): 5 x =1 8 >>> m = 9 9 >>> bar(m)10 >>> m11 912 >>>
一個程式的所有的變數並不是在哪個位置都可以訪問的。存取權限決定於這個變數是在哪裡賦值的。
變數的範圍決定了在哪一部分程式你可以訪問哪個特定的變數名稱。兩種最基本的變數範圍如下:
全域變數和局部變數
定義在函數內部的變數擁有一個局部範圍,定義在函數外的擁有全域範圍。
局部變數只能在其被聲明的函數內部訪問,而全域變數可以在整個程式範圍內訪問。調用函數時,所有在函數內聲明的變數名稱都將被加入到範圍中
1 >>> x =2 #全域變數 2 >>> def foo(): 3 x =9 #局部變數 4 print "this x is the fun:",x 5 6 7 >>> foo() 8 this x is the fun: 9 9 >>> x10 2
函數中的x 與函數外面的x,是不一樣的。我們把像x=9這樣的x,作用於某個函數體範圍內的,稱之為局部變數。11 >>> def bar(): 12 global x #在函數體中聲明全域變數13 x =914 print "this x is the fun:",x15 16 17 >>> x18 219 >>> bar()20 this x is the fun: 921 >>> x22 923 >>>
命名空間:命名空間是對範圍的一種特殊抽象
參數收集和傳值
收集方式1:*args
* args 是以元組的形式接收參數
1 >>> def foo(*arg): 2 print arg 3 4 5 >>> foo(1,2,3) 6 (1, 2, 3) #元組形式接收參數 7 >>> foo("baidu","ali","qq","weixin") 8 ('baidu', 'ali', 'qq', 'weixin') 9 >>> foo("web",[1,2,3,"pythom"])10 ('web', [1, 2, 3, 'pythom'])11 >>> def foo(x,*arg):12 print "x:",x13 print "arg:",arg14 15 16 >>> foo(1,2,3)17 x: 118 arg: (2, 3)19 >>> foo(7)20 x: 721 arg: ()22 >>>
收集方式2:**kargs 是以字典形式接收參數
1 >>> def foo(**karg): 2 print karg 3 4 5 >>> foo(a=1,b=2,c=3) 6 {'a': 1, 'c': 3, 'b': 2} 7 >>> def foo(x,*arg,**karg): 8 print x 9 print arg10 print karg11 12 13 >>> foo(1)14 115 ()16 {}17 >>> foo(1,2)18 119 (2,)20 {}21 >>> foo(1,2,3)22 123 (2, 3)24 {}25 >>> foo(1,2,3,name="python")26 127 (2, 3)28 {'name': 'python'}29 >>>
>>> def book(author,name): print "{0} has a book :{1}".format(author,name) >>> bars={"name":"learn python with cc","author":"cc"}>>> book(**bars)cc has a book :learn python with cc>>>
特殊函數
zip() 補充
1 >>> colors =["red","green","blue"] 2 >>> values=[234,12,89,65] 3 >>> zip(colors,values) 4 [('red', 234), ('green', 12), ('blue', 89)] 5 >>> dots=[(1,2),(3,4),(5,6)] 6 >>> x,y=zip(*dots) 7 >>> x 8 (1, 3, 5) 9 >>> y10 (2, 4, 6)11 >>> seq =range(1,10)12 >>> zip(*[iter(seq)]*3)13 [(1, 2, 3), (4, 5, 6), (7, 8, 9)]14 >>> x =iter(range(1,10))15 >>> x16 <listiterator object at 0x0000000003E8F860>17 >>> list(x)18 [1, 2, 3, 4, 5, 6, 7, 8, 9]19 >>> zip(x,x,x)20 []21 >>> x=iter(range(1,10))22 >>> zip(x,x,x)23 [(1, 2, 3), (4, 5, 6), (7, 8, 9)]24 >>>
lambda lambda x: x+y lambda 變數: 運算式
map、reduce、filter
1 >>> def foo(x): 2 x+-3 3 return x 4 5 >>> foo(4) 6 4 7 >>> n =range(10) 8 >>> n 9 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]10 >>> [i+3 for i in n ]11 [3, 4, 5, 6, 7, 8, 9, 10, 11, 12]12 >>> lam =lambda x:x+313 >>> n2=[]14 >>> for i in n15 SyntaxError: invalid syntax16 >>> for i in n:17 n2.append(lam(i))18 19 20 >>> n221 [3, 4, 5, 6, 7, 8, 9, 10, 11, 12]22 >>> g =lambda x,y:x+y23 >>> g(3,4)24 725 >>> lambda x:x+426 <function <lambda> at 0x0000000003E91438>27 >>> n28 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]29 >>> map(foo,n)30 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]31 >>> map(lambda x:x+3,n)32 [3, 4, 5, 6, 7, 8, 9, 10, 11, 12]33 >>> lst1 =[1,2,3,4,5]34 >>> lst2=[6,7,8,9,0]35 >>> map(lambda x,y:x+y,lst1,lst2)36 [7, 9, 11, 13, 5]37 >>> reduce(lambda x,y:x+y,lst1)38 1539 >>> n =range(-5,5)40 >>> n41 [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4]42 >>> filter(lambda x:x>0,n)43 [1, 2, 3, 4]44 >>> [x for x in n if x>0]45 [1, 2, 3, 4]46 >>>