Python 學習筆記(十三)Python函數(二)

參數和變量python

1 >>> def foo(a,b):  #函數是一個對象
2     return a+b
3 
4 >>> p =foo #對象賦值語句。將foo函數賦值給p這個變量
5 >>> foo(4,5)
6 9
7 >>> p(4,5) 變量p就指向了foo這個函數
8 9
9 >>> 

 按引用傳遞參數web

  按照順序進行賦值,函數中的變量a就指向了x,x是第一個實參,a這個參數指向了x所引用的對象,並非把3這個數複製一個放到函數中,這種調用對象的方式,稱之爲按引用傳遞。app

 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 >>> m
11 9
12 >>> 

  一個程序的全部的變量並非在哪一個位置均可以訪問的。訪問權限決定於這個變量是在哪裏賦值的。函數

  變量的做用域決定了在哪一部分程序你能夠訪問哪一個特定的變量名稱。兩種最基本的變量做用域以下:this

  全局變量和局部變量spa

  定義在函數內部的變量擁有一個局部做用域,定義在函數外的擁有全局做用域。code

  局部變量只能在其被聲明的函數內部訪問,而全局變量能夠在整個程序範圍內訪問。調用函數時,全部在函數內聲明的變量名稱都將被加入到做用域中orm

 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 >>> x
10 2
  函數中的x 與函數外面的x,是不同的。咱們把像x=9這樣的x,做用於某個函數體範圍內的,稱之爲局部變量。 11 >>> def bar(): 12 global x #在函數體中聲明全局變量 13 x =9 14 print "this x is the fun:",x 15 16 17 >>> x 18 2 19 >>> bar() 20 this x is the fun: 9 21 >>> x 22 9 23 >>>

命名空間:命名空間是對做用域的一種特殊抽象對象

參數收集和傳值blog

收集方式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:",x
13     print "arg:",arg
14 
15     
16 >>> foo(1,2,3)
17 x: 1
18 arg: (2, 3)
19 >>> foo(7)
20 x: 7
21 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 arg
10     print karg
11 
12     
13 >>> foo(1)
14 1
15 ()
16 {}
17 >>> foo(1,2)
18 1
19 (2,)
20 {}
21 >>> foo(1,2,3)
22 1
23 (2, 3)
24 {}
25 >>> foo(1,2,3,name="python")
26 1
27 (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 >>> y
10 (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 >>> x
16 <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+3
13 >>> n2=[]
14 >>> for i in n
15 SyntaxError: invalid syntax
16 >>> for i in n:
17     n2.append(lam(i))
18 
19     
20 >>> n2
21 [3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
22 >>> g =lambda x,y:x+y
23 >>> g(3,4)
24 7
25 >>> lambda x:x+4
26 <function <lambda> at 0x0000000003E91438>
27 >>> n
28 [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 15
39 >>> n =range(-5,5)
40 >>> n
41 [-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 >>> 
相關文章
相關標籤/搜索